Error message details display with Write-Debug

This commit is contained in:
Yorick Barbanneau 2018-05-02 22:57:36 +02:00
parent 88b48f5c66
commit d7329ab94e

View file

@ -35,7 +35,7 @@ function BlockHost {
Write-Host "`n`tBlock host $($params.host) : "
try {
if ( $(IsHostAlreadyBlocked $HOST_FILE $params.host) ){
#If host is inhosts.conf, verify that ip is blocked in FW
#If host is in hosts.conf, verify that ip is blocked in FW
if ( $params.ContainsKey('firewall') -and $params.firewall -eq $true ) {
$tmp = Get-Content $HOST_FILE | Where { $_ -ne "$HOST_IP`t`t$($params.host)" }
Set-Content $HOST_FILE $tmp
@ -53,8 +53,8 @@ function BlockHost {
}
}
catch {
Write-Host -NoNewline -ForegroundColor Red "`t`terror`n`t`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -ForegroundColor Red "`t`tError"
Write-Debug $Error[0].Exception.Message
}
}
else {
@ -74,8 +74,8 @@ function BlockHostByIP {
Write-Debug "Found a valid IP $ip"
$rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $ip } | Get-NetFirewallRule
if ( $rule ) {
write-host -NoNewLine "`tFW Rule exist : "
write-host -ForegroundColor yellow $rule.name
Write-Host -NoNewLine " exist "
Write-Host -ForegroundColor yellow $rule.name
}
else {
FwBlockOutputIP @{
@ -113,18 +113,18 @@ function FwBlockOutputIP {
Write-Host -NoNewline "`tAdd FW IP rule $name ($($params.ip)) : "
$rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $params.ip } | Get-NetFirewallRule
if ( $rule ) {
write-host -NoNewLine " exist : "
write-host -ForegroundColor yellow $rule.name
Write-Host -NoNewLine " exist "
Write-Host -ForegroundColor yellow $rule.name
}
else {
Try {
New-NetFirewallRule -Name "$name" -DisplayName "$name" -Direction Outbound -Protocol any -Enabled True -Profile Any -RemoteAddress $params.ip -Action Block | Out-Null
Write-Host -ForegroundColor Green "Done"
}
Catch {
Write-Host -ForegroundColor Red "error"
return
Write-Host -ForegroundColor Red "Error"
Write-Debug $Error[0].Exception.Message
}
Write-Host -ForegroundColor Green "done"
}
}
else {
@ -168,6 +168,7 @@ function FwBlockProgram {
}
catch {
Write-Host -ForegroundColor Red "error"
Write-Debug $Error[0].Exception.Message
}
}
else {
@ -206,8 +207,8 @@ function RemoveScheduledTask () {
Write-Host -ForegroundColor Yellow "`tScheduled Task $($params.path)$($params.name) not found"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError in RemoveSheduledTask`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -NoNewLine -ForegroundColor Red "`tError in RemoveSheduledTask"
Write-Debug $Error[0].Exception.Message
}
}
else {
@ -258,8 +259,8 @@ function AddRegKey {
New-Item -Path $params.path -Force | Out-Null
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "Error`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -NoNewLine -ForegroundColor Red "Error"
Write-Debug $Error[0].Exception.Message
return
}
}
@ -287,8 +288,8 @@ function AddRegKey {
Write-Host -ForegroundColor Red "Error (access denied)"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -NoNewLine -ForegroundColor Red "Error"
Write-Debug $Error[0].Exception.Message
}
}
@ -315,7 +316,7 @@ function DelRegKey {
}
Write-Host -NoNewline "`tDelete registery key $($params.key) : "
if ( ! (Test-Path $params.path) ){
Write-Host -ForegroundColor Red " Error (path not found)"
Write-Host -ForegroundColor Red "Error (path not found)"
return
}
try {
@ -333,8 +334,8 @@ function DelRegKey {
Write-Host -ForegroundColor Red "Error (access denied)"
}
catch {
Write-Host -ForegroundColor Red -NoNewLine "Error`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -ForegroundColor Red -NoNewLine "Error"
Write-Debug $Error[0].Exception.Message
}
}
@ -370,7 +371,8 @@ function DisableFeature {
Write-Host -ForegroundColor Green "done"
}
catch {
Write-Host -ForegroundColor Red "error"
Write-Host -ForegroundColor Red "Error"
Write-Debug $Error[0].Exception.Message
}
}
else {
@ -400,7 +402,7 @@ function UninstallModernApp {
UninstallModernApp @{name=$_}
}
$uninstall_list | Where-Object { $_ -notin $pkgs } | Foreach {
Write-Host -ForegroundColor Yellow "`tModern App $_ not installed"
Write-Debug "`tModern App $_ not installed"
}
}
elseif ( $params.ContainsKey('name') ) {
@ -410,8 +412,8 @@ function UninstallModernApp {
Write-Host -ForegroundColor Green "done"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "Error `n`t"
write-Host -ForegroundColor DarkRed $_
Write-Host -NoNewLine -ForegroundColor Red "Error"
Write-Debug $Error[0].Exception.Message
}
}
else {
@ -431,7 +433,6 @@ function UninstallModernProvisionedApp {
)]
[object]$params
)
if ( $params.ContainsKey('file') ) {
$pkgs = $(Get-AppxProvisionedPackage -Online).DisplayName
$list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" }
@ -439,7 +440,7 @@ function UninstallModernProvisionedApp {
UninstallModernProvisionedApp @{name=$_}
}
$list | Where-Object { $_ -notin $pkgs } | Foreach {
Write-Host -ForegroundColor Yellow "`tProvisioned App $_ not found"
Write-Debug "`tProvisioned App $_ not found"
}
}
elseif ( $params.ContainsKey('name') ){
@ -449,9 +450,8 @@ function UninstallModernProvisionedApp {
Write-Host -ForegroundColor Green "done"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
return
Write-Host -NoNewLine -ForegroundColor Red "`tError"
Write-Debug $Error[0].Exception.Message
}
}
else {
@ -479,7 +479,7 @@ function DisableService {
}
$list | Where-Object { $_ -notin $services } | Foreach {
Write-Host -ForegroundColor Yellow "`t Service $_ not found"
Write-Debug "`t Service $_ not found"
}
}
elseif ( $params.ContainsKey('name') ) {
@ -494,14 +494,13 @@ function DisableService {
return
}
try {
Stop-Service -InputObject $service
$service | Set-Service -StartupType disabled -ErrorAction Stop
Write-Host -ForegroundColor Green "done"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "Error`n`t"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -ForegroundColor Red "Error"
Write-Debug $Error[0].Exception.Message
}
finally {
if ( $params.ContainsKey('userService') -and $params.userService -eq $true ) {
@ -570,8 +569,8 @@ function DelFile {
Write-Host -ForegroundColor Green "done"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "`Error`n`t"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -ForegroundColor Red "`tError"
write-Debug $Error[0].Exception.Message
}
}
@ -601,7 +600,7 @@ function ExecCommand {
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Debug $Error[0].Exception.Message
}
}
@ -619,7 +618,7 @@ function ProcessModuleFile {
}
catch {
Write-Host -ForegroundColor Red "Error While Loading JSON : $path `n`n"
#Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Debug $Error[0].Exception.Message
return
}
Write-Host -ForegroundColor White "`nProcess Module $($mod.name) `n"
@ -671,11 +670,11 @@ try {
}
catch {
Write-Host -NoNewline -ForegroundColor Red "Error while mounting Registery`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Debug $Error[0].Exception.Message
return
}
#We need access to users registry hive for applying mofidication to existing users
#We need access to users registry hive for applying modifications to existing users
$profile_list = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"
Get-LocalUser | Where-Object { $_.Enabled -eq $true } | foreach {
$current_user_path = Get-ItemPropertyValue -Path "$profile_list$($_.SID.Value)\" -Name "ProfileImagePath"
@ -692,7 +691,7 @@ $script:users | foreach {
}
catch {
Write-Host -ForegroundColor Red "Error`n`t"
Write-host $Error[0].Exception.Message
Write-Debug $Error[0].Exception.Message
}
}
else {