support for custom logging
This commit is contained in:
+22
-11
@@ -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) {
|
Function Get-UninstallCodes ([string]$DisplayName) {
|
||||||
'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' | ForEach-Object {
|
'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' | ForEach-Object {
|
||||||
Get-ChildItem -Path $_ -ErrorAction SilentlyContinue | 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) ) {
|
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')
|
$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) {
|
Function Get-ProductKeys ([string]$ProductName) {
|
||||||
Get-ChildItem -Path 'HKCR:Installer\Products' | ForEach-Object {
|
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) ) {
|
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 Get-ServiceStatus ([string]$Name) { (Get-Service -Name $Name -ErrorAction SilentlyContinue).Status }
|
||||||
|
|
||||||
Function Stop-RunningService ([string]$Name) {
|
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) {
|
Function Remove-StoppedService ([string]$Name) {
|
||||||
$s = (Get-ServiceStatus -Name $Name)
|
$s = (Get-ServiceStatus -Name $Name)
|
||||||
If ( $s ) {
|
If ( $s ) {
|
||||||
If ( $s -eq "Stopped" ) {
|
If ( $s -eq "Stopped" ) {
|
||||||
Write-Output "Deleting : ${Name} service"
|
CLogMsg "Deleting : ${Name} service"
|
||||||
Start-Process "sc.exe" -ArgumentList "delete ${Name}" -Wait
|
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) {
|
Function Stop-RunningProcess ([string]$Name) {
|
||||||
$p = (Get-Process -Name $_ -ErrorAction SilentlyContinue)
|
$p = (Get-Process -Name $_ -ErrorAction SilentlyContinue)
|
||||||
If ( $p ) { Write-Output "Stopping : ${Name}.exe" ; $p | Stop-Process -Force }
|
If ( $p ) { CLogMsg "Stopping : ${Name}.exe" ; $p | Stop-Process -Force }
|
||||||
Else { Write-Output "Not Found: ${Name}.exe is not running"}
|
Else { CLogMsg "Not Found: ${Name}.exe is not running"}
|
||||||
}
|
}
|
||||||
|
|
||||||
Function Remove-Path ([string]$Path) {
|
Function Remove-Path ([string]$Path) {
|
||||||
If ( Test-Path $Path ) {
|
If ( Test-Path $Path ) {
|
||||||
Write-Output "Deleting : ${Path}"
|
CLogMsg "Deleting : ${Path}"
|
||||||
Remove-Item $Path -Recurse -Force
|
Remove-Item $Path -Recurse -Force
|
||||||
} Else { Write-Output "Not Found: ${Path}" }
|
} Else { CLogMsg "Not Found: ${Path}" }
|
||||||
}
|
}
|
||||||
|
|
||||||
Function Get-AllExeFiles ([string]$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
|
# Mount HKEY_CLASSES_ROOT registry hive
|
||||||
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
|
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
|
# 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
|
# Stop services if they're still running
|
||||||
$ServiceList | ForEach-Object { Stop-RunningService -Name $_ }
|
$ServiceList | ForEach-Object { Stop-RunningService -Name $_ }
|
||||||
|
|||||||
Reference in New Issue
Block a user