emergency fix for Uninstall-MSI

This commit is contained in:
2023-11-01 21:04:52 -04:00
parent 81bbde43ab
commit 14618526a7
+102 -93
View File
@@ -887,99 +887,108 @@ Function Get-GUIDFromMSIUninstallString ([string]$str) {
Return $retval Return $retval
} }
#Function Uninstall-MSI { Function Get-GUIDFromMSIUninstallString ([string]$str) {
# param( $start = $str.IndexOf('{') + 1
# [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string[]]$APP_NAMES, $end = $str.IndexOf('}')
# [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Match, $retval = $str.Substring($start, $end - $start)
# [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$IncludeNonMSI If ( $retval.Length -ne 36 ) { $retval = $null }
# ) Return $retval
# BEGIN { }
# # Get all of the uninstall registry keys Function Uninstall-MSI {
# $UNINSTALL_KEYS = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -ErrorAction SilentlyContinue param(
# $UNINSTALL_KEYS += Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall -ErrorAction SilentlyContinue [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string[]]$APP_NAMES,
# [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Match,
# # Exit if we can't find any software at all [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$IncludeNonMSI
# If ( $UNINSTALL_KEYS.Length -eq 0 ) { Write-Error "Could not find any installed apps in the Windows Registry" ; Return } )
# BEGIN {
# # Array to store custom objects about each installed MSI app # Get all of the uninstall registry keys
# $INSTALLED_APPS = @() $UNINSTALL_KEYS = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -ErrorAction SilentlyContinue
# $UNINSTALL_KEYS += Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall -ErrorAction SilentlyContinue
# Foreach ( $reg in $UNINSTALL_KEYS ) {
# # Get the uninstall key # Exit if we can't find any software at all
# $REG_PROPERTY = Get-ItemProperty -Path $reg.PSPath If ( $UNINSTALL_KEYS.Length -eq 0 ) { Write-Error "Could not find any installed apps in the Windows Registry" ; Return }
#
# # Skip apps missing both the UninstallString and ModifyPath # Array to store custom objects about each installed MSI app
# If ( [string]::IsNullOrEmpty($REG_PROPERTY.UninstallString) -and [string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { Continue } $INSTALLED_APPS = @()
#
# # Determine if the app is installed via MSI Foreach ( $reg in $UNINSTALL_KEYS ) {
# If ( $REG_PROPERTY.WindowsInstaller -eq 1 ) { # Get the uninstall key
# $REG_PROPERTY = Get-ItemProperty -Path $reg.PSPath
# $MSI = $true
# # Skip apps missing both the UninstallString and ModifyPath
# # Get the product code from the UninstallString or ModifyPath if the app is installed via MSI If ( [string]::IsNullOrEmpty($REG_PROPERTY.UninstallString) -and [string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { Continue }
# If ( ![string]::IsNullOrEmpty($REG_PROPERTY.UninstallString) ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $REG_PROPERTY.UninstallString) }
# # Determine if the app is installed via MSI
# # Get the product code from the ModifyPath If ( $REG_PROPERTY.WindowsInstaller -eq 1 ) {
# ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $REG_PROPERTY.ModifyPath) }
# $MSI = $true
# # Don't add an app with an unknown product code
# If ( [string]::IsNullOrEmpty($PRODUCT_CODE) ) { Continue } # Get the product code from the UninstallString or ModifyPath if the app is installed via MSI
# If ( ![string]::IsNullOrEmpty($REG_PROPERTY.UninstallString) ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $REG_PROPERTY.UninstallString) }
# } Else { $MSI = $false }
# # Get the product code from the ModifyPath
# # Get a usable name for the entry ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $REG_PROPERTY.ModifyPath) }
# If ( ![string]::IsNullOrEmpty($REG_PROPERTY.DisplayName) ) {
# $AppName = $REG_PROPERTY.DisplayName # Don't add an app with an unknown product code
# } ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.Name) ) { If ( [string]::IsNullOrEmpty($PRODUCT_CODE) ) { Continue }
# $AppName = $REG_PROPERTY.DisplayName
# } Else { } Else { $MSI = $false }
# $AppName = '[unknown]'
# } # Get a usable name for the entry
# If ( ![string]::IsNullOrEmpty($REG_PROPERTY.DisplayName) ) {
# $customObject = [PSCustomObject]@{ $AppName = $REG_PROPERTY.DisplayName
# MSI = $MSI } ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.Name) ) {
# DisplayName = $AppName $AppName = $REG_PROPERTY.DisplayName
# UninstallString = $REG_PROPERTY.UninstallString } Else {
# ModifyPath = $REG_PROPERTY.ModifyPath $AppName = '[unknown]'
# ProductCode = $PRODUCT_CODE }
# }
# $customObject = [PSCustomObject]@{
# # Cleanup object properties MSI = $MSI
# #If ( $customObject.) DisplayName = $AppName
# UninstallString = $REG_PROPERTY.UninstallString
# $INSTALLED_APPS += $customObject ModifyPath = $REG_PROPERTY.ModifyPath
# ProductCode = $PRODUCT_CODE
# } }
# }
# PROCESS { # TODO: Cleanup object properties
# Foreach ( $app in $APP_NAMES ) {
# Foreach ( $installed_app in $INSTALLED_APPS ) { $INSTALLED_APPS += $customObject
#
# # Do not add apps which name does not match the one we're looking for }
# Switch ( $Match ) { }
# $true { If ( !($installed_app.DisplayName -match $app) ) { Continue } } PROCESS {
# $false { If ( !($installed_app.DisplayName -ilike $app) ) { Continue } } Foreach ( $app in $APP_NAMES ) {
# } Foreach ( $installed_app in $INSTALLED_APPS ) {
# $uninstall = $false
# $DisplayName = $installed_app.DisplayName
# $UninstallString = $installed_app.UninstallString # Do not add apps whose name does not match the one we're looking for
# $ProductCode = $installed_app.ProductCode Switch ( $Match ) {
# $true { If ( $installed_app.DisplayName -match $app ) { $uninstall = $true } }
# # Print out uninstall string for unsupported apps $false { If ( $installed_app.DisplayName -ilike $app ) { $uninstall = $true } }
# If ( $installed_app.UninstallSupported -eq $false ) { }
# Write-Output "Cannot uninstall ""${DisplayName}""`n UninstallString: ${UninstallString}"
# Continue # Print out uninstall string for unsupported apps
# } If ( ($uninstall) -and ($installed_app.UninstallSupported -eq $false) ) {
# Write-Output "Cannot uninstall ""${DisplayName}""`n UninstallString: ${UninstallString}"
# # Uninstall the app $uninstall = $false
# Write-Output "Uninstalling ""${DisplayName}"" with product code: ${ProductCode}" }
# $arguments = '/X{' + $ProductCode + '} /qn /norestart'
# Try { Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait } If ( $uninstall ) {
# Catch { Write-Error $_.Exception.Message } $DisplayName = $installed_app.DisplayName
# } $UninstallString = $installed_app.UninstallString
# } $ProductCode = $installed_app.ProductCode
# }
#} # Uninstall the app
Write-Output "Uninstalling ""${DisplayName}"" with product code: ${ProductCode}"
$arguments = '/X{' + $ProductCode + '} /qn /norestart'
Try { Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait }
Catch { Write-Error $_.Exception.Message }
}
}
}
}
}
# Reboot an endpoint based on the amount of time it has been running without a reboot # Reboot an endpoint based on the amount of time it has been running without a reboot
Function Restart-EndpointOnUptime ([timespan]$MaxUptime) { Function Restart-EndpointOnUptime ([timespan]$MaxUptime) {