Remove-MsiByGUID

Topics: Archive - Toolkit Extensions
Apr 28, 2014 at 9:05 AM
Mighty all, I have started working on a function to remove MSIs by GUIDs, as sometimes in our environment applications do not register by app name, but only by MSI GUIDs. This is an early cut, so feel free to contribute :)
function Remove-MsiByGUID {
<#
.SYNOPSIS
       Performs removal of a particular GUID
.DESCRIPTION
       Changes to user install mode for Remote Desktop Session Host/Citrix servers
.EXAMPLE
       Remove-MsiByGUID -GUID "{594ABD08-C02C-4827-8ABF-A128006848A3}"
.NOTES
.LINK
       Http://psappdeploytoolkit.codeplex.com
#>
       
       Param (

    [Parameter(Mandatory=$True, HelpMessage="GUIDs to remove if a per-user installation will be detected")] 
       [ValidateNotNullOrEmpty()]
       [ValidatePattern('\A\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}\Z')]
       [string]$GUID = $null,
       
       [int]$Retries = $null
       
       )
       
$logpath="$env:LogPath\installs"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "$env:SystemRoot\System32\msiexec.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "/x $GUID /qb /l*v $LogPath\$GUID.log"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
       
$i=1
if ($Retries -eq $null) {$Retries = 2}
       
       do {
Write-Log -Text "Starting attempt # $i to remove $guid."      

$p.Start() | Out-Null
Write-Log -Text "Removal started, waiting for process to terminate." 
$p.WaitForExit()     
Write-Log -Text "Process stopped."
$result = $p.ExitCode      
Write-Log -Text "Return code was $result"
if ($result -eq "3010"){Write-Log -Text "Information: Process returned 3010, consider rebooting, would you AI??."}

$i++
              
              } until (($i = $Retries) -or ($result -eq "1605") -or ($result -eq "1603"))

if ($result -eq "1603"){Write-Log -Text "Process returned 1603 - stopping further attempts to remove."}
elseif ($i -eq "3") {Write-Log -Text "Tried to remove $GUID 2 times, gving up"}

       
       
}
Coordinator
Apr 30, 2014 at 6:01 PM
Nice job :)
May 2, 2014 at 6:55 AM
Dear all,

sorry for asking, but what is the difference between this function and "Execute-MSI -Action Uninstall -Path..." ?

It isn't clear for me :-(

Regards,
Maximilian
May 2, 2014 at 8:35 AM
For example MSI could be no longer available on the target system