From ecc1d42a0409bbb262434ac833b453e771dd8d96 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Sat, 16 Apr 2022 10:21:36 -0400 Subject: [PATCH] support for custom logging --- Uninstall-AteraAgent.ps1 | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/Uninstall-AteraAgent.ps1 b/Uninstall-AteraAgent.ps1 index 367d1e5..0f70efa 100644 --- a/Uninstall-AteraAgent.ps1 +++ b/Uninstall-AteraAgent.ps1 @@ -1,10 +1,16 @@ - +Function CLogMsg { + param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message) + If ( $CUSTOM_LOG_AVAILABLE ) { LogMsg $Message } Else { CLogMsg $Message } +} + Function Get-UninstallCodes ([string]$DisplayName) { 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' | ForEach-Object { Get-ChildItem -Path $_ -ErrorAction SilentlyContinue | ForEach-Object { If ( $(Get-ItemProperty -Path $_.PSPath -Name 'DisplayName' -ErrorAction SilentlyContinue) -and ($(Get-ItemPropertyValue -Path $_.PSPath -Name 'DisplayName' -ErrorAction SilentlyContinue) -eq $DisplayName) ) { $str = (Get-ItemPropertyValue -Path $_.PSPath -Name 'UninstallString') - $UninstallCodes.Add($str.Substring(($str.Length - 37),36)) | Out-Null + $code = $str.Substring(($str.Length - 37),36) + CLogMsg "Found : ${code}" + $UninstallCodes.Add($code) | Out-Null } } } @@ -13,7 +19,9 @@ Function Get-UninstallCodes ([string]$DisplayName) { Function Get-ProductKeys ([string]$ProductName) { Get-ChildItem -Path 'HKCR:Installer\Products' | ForEach-Object { If ( $(Get-ItemProperty -Path $_.PSPath -Name 'ProductName' -ErrorAction SilentlyContinue) -and ($(Get-ItemPropertyValue -Path $_.PSPath -Name 'ProductName' -ErrorAction SilentlyContinue) -eq $ProductName) ) { - $ProductKeys.Add($_.PSPath.Substring(($_.PSPath.Length - 32))) | Out-Null + $prod = $_.PSPath.Substring($_.PSPath.Length - 32) + CLogMsg = "Found : ${prod}" + $ProductKeys.Add($prod) | Out-Null } } } @@ -21,30 +29,30 @@ Function Get-ProductKeys ([string]$ProductName) { Function Get-ServiceStatus ([string]$Name) { (Get-Service -Name $Name -ErrorAction SilentlyContinue).Status } Function Stop-RunningService ([string]$Name) { - If ( $(Get-ServiceStatus -Name $Name) -eq "Running" ) { Write-Output "Stopping : ${Name} service" ; Stop-Service -Name $Name -Force } + If ( $(Get-ServiceStatus -Name $Name) -eq "Running" ) { CLogMsg "Stopping : ${Name} service" ; Stop-Service -Name $Name -Force } } Function Remove-StoppedService ([string]$Name) { $s = (Get-ServiceStatus -Name $Name) If ( $s ) { If ( $s -eq "Stopped" ) { - Write-Output "Deleting : ${Name} service" + CLogMsg "Deleting : ${Name} service" Start-Process "sc.exe" -ArgumentList "delete ${Name}" -Wait } - } Else { Write-Output "Not Found: ${Name} service" } + } Else { CLogMsg "Not Found: ${Name} service" } } Function Stop-RunningProcess ([string]$Name) { $p = (Get-Process -Name $_ -ErrorAction SilentlyContinue) - If ( $p ) { Write-Output "Stopping : ${Name}.exe" ; $p | Stop-Process -Force } - Else { Write-Output "Not Found: ${Name}.exe is not running"} + If ( $p ) { CLogMsg "Stopping : ${Name}.exe" ; $p | Stop-Process -Force } + Else { CLogMsg "Not Found: ${Name}.exe is not running"} } Function Remove-Path ([string]$Path) { If ( Test-Path $Path ) { - Write-Output "Deleting : ${Path}" + CLogMsg "Deleting : ${Path}" Remove-Item $Path -Recurse -Force - } Else { Write-Output "Not Found: ${Path}" } + } Else { CLogMsg "Not Found: ${Path}" } } Function Get-AllExeFiles ([string]$Path) { @@ -53,6 +61,9 @@ Function Get-AllExeFiles ([string]$Path) { } } +# Test if the custom function LogMsg is available in the current scope +If ( Get-Command 'LogMsg' -ErrorAction SilentlyContinue ) { $CUSTOM_LOG_AVAILABLE = $true } Else { $CUSTOM_LOG_AVAILABLE = $false } + # Mount HKEY_CLASSES_ROOT registry hive New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null @@ -87,7 +98,7 @@ $RegistryKeys = @('HKLM:SOFTWARE\ATERA Networks') ####### # Uninstall each MSI package code in $UninstallCodes -$UninstallCodes | ForEach-Object { Write-Output "Uninstall: ${_}" ; Start-Process "msiexec.exe" -ArgumentList "/X{${_}} /qn" -Wait } +$UninstallCodes | ForEach-Object { CLogMsg "Uninstall: ${_}" ; Start-Process "msiexec.exe" -ArgumentList "/X{${_}} /qn" -Wait } # Stop services if they're still running $ServiceList | ForEach-Object { Stop-RunningService -Name $_ }