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