Skip to content

Instantly share code, notes, and snippets.

@pinecones-sx
Last active March 1, 2017 18:36
Show Gist options
  • Save pinecones-sx/9f837d53a2508de81f65aa0768344a8a to your computer and use it in GitHub Desktop.
Save pinecones-sx/9f837d53a2508de81f65aa0768344a8a to your computer and use it in GitHub Desktop.
current SCCM-based uninstaller
<#
This checks for non-current copies of SysAid & uninstalls them.
FILES:
Uninstall-SysAid.ps1
This file. Must be run with execution policy bypassed:
powershell.exe -executionpolicy bypass -file .\Uninstall-SysAid.ps1
SysAidAgentx64.msi
This is the "install" file. We're using it with uninstall
switches to remove existing copies of SysAid.
SysAidWorker.exe
This is the reference file of the current properly installed
version of SysAid. If a new version of SysAid comes out, just
replace this file with the newest one.
#>
# Function to convert version number to decimal
function Get-VersionAsDec {
param($VersionInfo,[switch]$FileVersion)
$digits = 6
If ($FileVersion){$iVersionNumber = $VersionInfo.FileVersion}
Else{$iVersionNumber = $VersionInfo.ProductVersion}
$rVersionDec = ''
ForEach ($versionPart in ($iVersionNumber -split '\.')){
$lessDigits = $digits - $versionPart.Length
For ($i = 0 ; $i -lt $lessDigits ; $i++){
$versionPart = '0' + $versionPart
}
$rVersionDec = $rVersionDec + [string]$versionPart
}
$rVersionDec = [decimal]($rVersionDec.TrimStart('0'))
Return $rVersionDec
}
# Clear error variable
$error.clear()
# MSI install verification info
$productName = 'sysaid'
$productCode = '{FC5E1D1D-6D3F-4844-A937-567D589F655E}'
# File names
$msiName = 'SysAidAgentx64.msi'
$currentExecFileName = 'SysAidWorker.exe'
$badSysAidFolderUNC = 'C:\Program Files\SysAid (x86)\'
$sysAidFolderUNC = 'C:\Program Files\SysAid\'
$sysAidEXEUNC = 'C:\Program Files\SysAid\SysAidWorker.exe'
# Normalize installer name
$msiUninstaller = Get-Item ('.\' + $msiName) -Force | Select -ExpandProperty FullName
# Get example EXE information
$currentVerEXE = Get-Item ('.\' + $currentExecFileName) -Force
 
If (-not $error){
# Set failure variable
$uninstallNeeded = $false
# Test to make sure there wasn't a previous x86 install
$badSysAidExists = Get-Item $badSysAidFolderUNC -Force -ErrorAction Ignore
If ($badSysAidExists){$uninstallNeeded = $true}
# Test to see if there is an existing install folder
If (-not $uninstallNeeded){
$sysAidFolder = Get-Item $sysAidFolderUNC -Force -ErrorAction Ignore
If (-not $sysAidFolder){$uninstallNeeded = $true}
Else{
$sysAidInstalledEXE = Get-Item $sysAidEXEUNC -Force -ErrorAction Ignore
If(-not $sysAidInstalledEXE){$uninstallNeeded = $true}
Else{
# Compare version numbers
$refVer = Get-VersionAsDec ($currentVerEXE.VersionInfo)
$insVer = Get-VersionAsDec ($sysAidInstalledEXE.VersionInfo)
If ($insVer -lt $refVer){$uninstallNeeded = $true}
}
}
}
 
# If evidence of any old installs found, run uninstaller (or if no evidence)
If ($uninstallNeeded){
# Call uninstall method (MSI)
$cmdCommand = (
('/x ' + $msiUninstaller),
'/qn',
'/norestart'
)
#Start-Process -FilePath 'MSIEXEC.EXE' -ArgumentList $cmdCommand -NoNewWindow -Wait
Write-Verbose 'Call uninstaller.'
# Verify we can't find application
$uninstallFailed = (
Get-Item $sysAidEXEUNC -Force -ErrorAction Ignore |
Where {
(Get-VersionAsDec $_.VersionInfo) -lt (Get-VersionAsDec $currentVerEXE.VersionInfo)
} -ErrorAction Ignore
)
}
}
 
# If $installSuccessful isn't empty, report success, otherwise failure
If ((-not $uninstallFailed) -and (-not $error)){
# Must write something (anything) to host
Write-Host ('Success!')
Exit 0
}
Else{
# this condition returns a failure
Clear-Host
Exit 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment