KACE Uninstall Script for Windows

Here is a worklet to assist with KACE agent uninstallation.

The agent installation presents a few challenges, as it is a 32 bit installer, so it can be installed either in program files, or in program files (x86) depending on the OS architecture. In addition based on the Quest installation instructions, it will be created in one of two different sub-directories based on OS version and client version.

This worklet is designed to work on either 32-bit or 64-bit Windows devices, and will work as long as the AMPTools.exe file exists in one of the default locations.
We have an awesome customer giving this a shot, and I will update the code here if we find anything needs to be changed.

Evaluation code borrowed from the Worklet: Enforced Application Uninstall for Windows

Evaluation Code:

<#
.SYNOPSIS
  Check for presence of specified application on the target device

.DESCRIPTION
  Read 32-bit and 64-bit registry to find matching applications

  Exits with 0 for compliance, 1 for Non-Compliance. 
  Non-Compliant devices will run Remediation Code at the Policy's next scheduled date.

.NOTES
  A scriptblock is used to workaround the limitations of 32-bit powershell.exe.
  This allows us to redirect the operations to a 64-bit powershell.exe and read
  the 64-bit registry without .NET workarounds.

.LINK
http://www.automox.com
#>

# The ScriptBlock method used here is to allow a 32-bit agent process
# to access the 64-bit registry on 64-bit Windows. This is necessary if the application
# isn't known to be 32-bit only.


$scriptblock = {
    #Define Registry Location for the 64-bit and 32-bit Uninstall keys
    $uninstReg = @('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')

    # Define the App Name to look for
    # Look at a machine with the application installed unless you're sure the formatting of the name/version
    # Specifically the DisplayName. This is what you see in Add/Remove Programs. This doesn't have to be exact.
    # Default behavior uses -match which is essentially "DisplayName contains VLC"
    ##################
    $appName = 'KACE'
    ##################

    # Get all entries that match our criteria. DisplayName matches $appname
    $installed = @(Get-ChildItem $uninstReg -ErrorAction SilentlyContinue | Get-ItemProperty | Where-Object { ($_.DisplayName -match $appName) })

    # If any matches were present, $installed will be populated. If none, then $installed is NULL and this IF statement will be false.
    # The return value here is what the ScriptBlock will send back to us after we run it.
    # 1 for Non-Compliant, 0 for Compliant
    if ($installed) {
        return 1
    } else { return 0 }
}


$exitCode = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock
Exit $exitCode

Remediation Code:

<#
.SYNOPSIS
    This script allows an admin to uninstall KACE
.DESCRIPTION
    This script runs the exe installer with uninstall switchs from a predetermined directory.
    This script may not work on all systems. Modify to fit your needs
.NOTES
    File Name       :KACE_Uninstall.ps1
    Author          :Automox
    Prerequisite    :PowerShell V2 over win7 and upper
.LINK
    http://www.automox.com
#>

function Kace_Uninstall_Rem {

    <#
    .SYNOPSIS
        This function allows automox to trigger an Uninstall KACE on Windows from up to two predetermined paths.
    .DESCRIPTION
        This function uninstalls KACE on system.
    .EXAMPLE
        Kace_Uninstall_Rem
    .NOTES
    #>

    ############# Change the settings in this block #######################
    # KACE executable name.
    $ExeFile = "AMPTools.exe"
    # Check if file is in Program Files or Program Files(x86) and then this sub directory 
    $FilePath1 = "\Quest\KACE\"
    # Potential secondary installation path.  Check if file is in Program Files or Program Files(x86) and then this sub directory 
    $FilePath2 = "\Dell\KACE\"
    #######################################################################

    #define $SaveFilePath Program Files or Program Files(x86) to simplify multiple OS architectures
    if ([System.Environment]::Is64BitOperatingSystem){
        $TestPath1 = ${Env:ProgramFiles(x86)} + $FilePath1 + $ExeFile
        $TestPath2 = ${Env:ProgramFiles(x86)} + $FilePath2 + $ExeFile
        if(Test-Path $TestPath1){
            $SaveFilePath = $TestPath1
        } 
        elseif(Test-Path $TestPath2){
            $SaveFilePath = $TestPath2}    } 
    elseif (![System.Environment]::Is64BitOperatingSystem){      
        $TestPath1 = ${Env:ProgramFiles} + $FilePath1 + $ExeFile
        $TestPath2 = ${Env:ProgramFiles} + $FilePath2 + $ExeFile
        } if(Test-Path $TestPath1){
            $SaveFilePath = $TestPath1
    }   elseif(Test-Path $TestPath2){
            $SaveFilePath = $TestPath2
    } else{
        $host.ui.WriteErrorLine("$ExeFile does not exist at $FilePath1 or $FilePath2")
        exit 1
    }
  
    # Uninstall KACE
    try {Start-Process -FilePath $saveFilePath -ArgumentList "uninstall all-kuid" -PassThru -ErrorAction Stop
         Write-Output "KACE uninstall Finished...`n"
    }
    catch { $Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber
            Write-Error $Exception
            exit 90
    }
}
Kace_Uninstall_Rem
1 Like