added new function Get-GUIDFromMSIUninstallString

added new function Uninstall-MSI
This commit is contained in:
2023-08-22 16:09:13 -04:00
parent 3f36b91570
commit f6ebc2f0fa
+55
View File
@@ -795,5 +795,60 @@ Function Get-AbsoluteURI {
Return [System.Net.HttpWebRequest]::Create($URL).GetResponse().ResponseUri.AbsoluteUri
}
Function Get-GUIDFromMSIUninstallString ([string]$str) {
$start = $str.IndexOf('{') + 1
$end = $str.IndexOf('}')
Return $str.Substring($start, $end - $start)
}
Function Uninstall-MSI ([string]$APP_NAME) {
# Get all of the uninstall registry keys
$UNINSTALL_KEYS = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
$UNINSTALL_KEYS += Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
# Track whether an app was uninstalled or not
$PRODUCTUNINSTALLED = $false
Foreach ( $reg in $UNINSTALL_KEYS ) {
# Get the uninstall key
$REG_PROPERTY = Get-ItemProperty -Path $reg.PSPath
# Get the display name of the app
$DISPLAY_NAME = $REG_PROPERTY.DisplayName
# Get the UninstallString
$UNINSTALLSTRING = $REG_PROPERTY.UninstallString
# Get the ModifyPath
$MODIFYPATH = $REG_PROPERTY.ModifyPath
# Make sure the DisplayName matches what we're looking for
If ( $DISPLAY_NAME -ilike $APP_NAME ) {
# Make sure the software was installed using an MSI
If ( $REG_PROPERTY.WindowsInstaller -eq 1 ) {
# Get the product code from the UninstallString
If ( $UNINSTALLSTRING ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $UNINSTALLSTRING) }
# Get the product code from the ModifyPath
ElseIf ( $MODIFYPATH ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $MODIFYPATH) }
# Uninstall the app
If ( $PRODUCT_CODE ) {
LogMsg "Uninstalling ${DISPLAY_NAME} ${PRODUCT_CODE}"
$arguments = '/X{' + $PRODUCT_CODE + '} /qn /norestart'
Try { Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait ; $PRODUCTUNINSTALLED = $true }
Catch { LogErr $_.Exception.Message }
}
}
Else {
LogMsg "Cannot uninstall ""${DISPLAY_NAME}""`n UninstallString: ${UNINSTALLSTRING}"
}
}
}
If ( $PRODUCTUNINSTALLED -eq $false ) { LogMsg "No product matching ${APP_NAME} was found"}
}
Setup-MSPDirectories
Setup-LogFile