possibly an emergency

This commit is contained in:
2023-11-01 14:30:39 -04:00
parent 7b77fc85d9
commit 9a94618799
+93 -93
View File
@@ -892,99 +892,99 @@ Function Get-GUIDFromMSIUninstallString ([string]$str) {
Return $retval Return $retval
} }
Function Uninstall-MSI { #Function Uninstall-MSI {
param( # param(
[Parameter(ValueFromPipeline=$true,Mandatory=$true)][string[]]$APP_NAMES, # [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string[]]$APP_NAMES,
[Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Match, # [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Match,
[Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$IncludeNonMSI # [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$IncludeNonMSI
) # )
BEGIN { # BEGIN {
# Get all of the uninstall registry keys # # Get all of the uninstall registry keys
$UNINSTALL_KEYS = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -ErrorAction SilentlyContinue # $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 # $UNINSTALL_KEYS += Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall -ErrorAction SilentlyContinue
#
# Exit if we can't find any software at all # # Exit if we can't find any software at all
If ( $UNINSTALL_KEYS.Length -eq 0 ) { Write-Error "Could not find any installed apps in the Windows Registry" ; Return } # If ( $UNINSTALL_KEYS.Length -eq 0 ) { Write-Error "Could not find any installed apps in the Windows Registry" ; Return }
#
# Array to store custom objects about each installed MSI app # # Array to store custom objects about each installed MSI app
$INSTALLED_APPS = @() # $INSTALLED_APPS = @()
#
Foreach ( $reg in $UNINSTALL_KEYS ) { # Foreach ( $reg in $UNINSTALL_KEYS ) {
# Get the uninstall key # # Get the uninstall key
$REG_PROPERTY = Get-ItemProperty -Path $reg.PSPath # $REG_PROPERTY = Get-ItemProperty -Path $reg.PSPath
#
# Skip apps missing both the UninstallString and ModifyPath # # Skip apps missing both the UninstallString and ModifyPath
If ( [string]::IsNullOrEmpty($REG_PROPERTY.UninstallString) -and [string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { Continue } # If ( [string]::IsNullOrEmpty($REG_PROPERTY.UninstallString) -and [string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { Continue }
#
# Determine if the app is installed via MSI # # Determine if the app is installed via MSI
If ( $REG_PROPERTY.WindowsInstaller -eq 1 ) { # If ( $REG_PROPERTY.WindowsInstaller -eq 1 ) {
#
$MSI = $true # $MSI = $true
#
# Get the product code from the UninstallString or ModifyPath if the app is installed via MSI # # 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) } # If ( ![string]::IsNullOrEmpty($REG_PROPERTY.UninstallString) ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $REG_PROPERTY.UninstallString) }
#
# Get the product code from the ModifyPath # # Get the product code from the ModifyPath
ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $REG_PROPERTY.ModifyPath) } # ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.ModifyPath) ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $REG_PROPERTY.ModifyPath) }
#
# Don't add an app with an unknown product code # # Don't add an app with an unknown product code
If ( [string]::IsNullOrEmpty($PRODUCT_CODE) ) { Continue } # If ( [string]::IsNullOrEmpty($PRODUCT_CODE) ) { Continue }
#
} Else { $MSI = $false } # } Else { $MSI = $false }
#
# Get a usable name for the entry # # Get a usable name for the entry
If ( ![string]::IsNullOrEmpty($REG_PROPERTY.DisplayName) ) { # If ( ![string]::IsNullOrEmpty($REG_PROPERTY.DisplayName) ) {
$AppName = $REG_PROPERTY.DisplayName # $AppName = $REG_PROPERTY.DisplayName
} ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.Name) ) { # } ElseIf ( ![string]::IsNullOrEmpty($REG_PROPERTY.Name) ) {
$AppName = $REG_PROPERTY.DisplayName # $AppName = $REG_PROPERTY.DisplayName
} Else { # } Else {
$AppName = '[unknown]' # $AppName = '[unknown]'
} # }
#
$customObject = [PSCustomObject]@{ # $customObject = [PSCustomObject]@{
MSI = $MSI # MSI = $MSI
DisplayName = $AppName # DisplayName = $AppName
UninstallString = $REG_PROPERTY.UninstallString # UninstallString = $REG_PROPERTY.UninstallString
ModifyPath = $REG_PROPERTY.ModifyPath # ModifyPath = $REG_PROPERTY.ModifyPath
ProductCode = $PRODUCT_CODE # ProductCode = $PRODUCT_CODE
} # }
#
# Cleanup object properties # # Cleanup object properties
#If ( $customObject.) # #If ( $customObject.)
#
$INSTALLED_APPS += $customObject # $INSTALLED_APPS += $customObject
#
} # }
} # }
PROCESS { # PROCESS {
Foreach ( $app in $APP_NAMES ) { # Foreach ( $app in $APP_NAMES ) {
Foreach ( $installed_app in $INSTALLED_APPS ) { # Foreach ( $installed_app in $INSTALLED_APPS ) {
#
# Do not add apps which name does not match the one we're looking for # # Do not add apps which name does not match the one we're looking for
Switch ( $Match ) { # Switch ( $Match ) {
$true { If ( !($installed_app.DisplayName -match $app) ) { Continue } } # $true { If ( !($installed_app.DisplayName -match $app) ) { Continue } }
$false { If ( !($installed_app.DisplayName -ilike $app) ) { Continue } } # $false { If ( !($installed_app.DisplayName -ilike $app) ) { Continue } }
} # }
#
$DisplayName = $installed_app.DisplayName # $DisplayName = $installed_app.DisplayName
$UninstallString = $installed_app.UninstallString # $UninstallString = $installed_app.UninstallString
$ProductCode = $installed_app.ProductCode # $ProductCode = $installed_app.ProductCode
#
# Print out uninstall string for unsupported apps # # Print out uninstall string for unsupported apps
If ( $installed_app.UninstallSupported -eq $false ) { # If ( $installed_app.UninstallSupported -eq $false ) {
Write-Output "Cannot uninstall ""${DisplayName}""`n UninstallString: ${UninstallString}" # Write-Output "Cannot uninstall ""${DisplayName}""`n UninstallString: ${UninstallString}"
Continue # Continue
} # }
#
# Uninstall the app # # Uninstall the app
Write-Output "Uninstalling ""${DisplayName}"" with product code: ${ProductCode}" # Write-Output "Uninstalling ""${DisplayName}"" with product code: ${ProductCode}"
$arguments = '/X{' + $ProductCode + '} /qn /norestart' # $arguments = '/X{' + $ProductCode + '} /qn /norestart'
Try { Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait } # Try { Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait }
Catch { Write-Error $_.Exception.Message } # 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) {