Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit of the Windows uninstall script #610

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions scripts/windows/UninstallWindowsAgent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Function to check if Powershell is running as Administrator
function Test-Administrator {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

if (-not (Test-Administrator)) {
Write-Warning "This script needs to be run as an administrator."
exit
}

# Create HKCR Mapping
New-PSDrive -Name "HKCR" -PSProvider Registry -Root "HKEY_CLASSES_ROOT" -ErrorAction SilentlyContinue

# Function to recursively search for DisplayName in the registry
function Find-JumpCloudGUID {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When pulling the GUID, is it possible to have Multiple JCAgent GUIDs?
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the value with brackets MSI installation ID? It looks like it from HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall<JC MSI Installation ID>. If so, pulling the MSI installation ID should be in a separate function from Find-JumpCloudGUID.

$rootKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"
$uninstallKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
$jumpcloudGUIDs = @()

# Searching Installed Products
Get-ChildItem -Path $rootKey | ForEach-Object {
$installPropertiesPath = "$($_.PSPath)\InstallProperties"
try {
$displayName = (Get-ItemProperty -Path $installPropertiesPath -ErrorAction SilentlyContinue).DisplayName
if ($displayName -like "*JumpCloud Agent*") {
$guid = $_.PSChildName
$jumpcloudGUIDs += $guid
}
} catch {
Write-Host "Error accessing $installPropertiesPath"
}
}

# Searching for the Uninstall Key
Get-ChildItem -Path $uninstallKey | ForEach-Object {
try {
$displayName = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).DisplayName
if ($displayName -like "*JumpCloud Agent*") {
$uninstallGUID = $_.PSChildName
$jumpcloudGUIDs += $uninstallGUID
}
} catch {
Write-Host "Error accessing $($_.PSPath)"
}
}

return $jumpcloudGUIDs
}

# Function to remove registry keys, JC folder, and JC service
function Remove-JumpCloud {
$guids = Find-JumpCloudGUID

foreach ($guid in $guids) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got GUID and MSI Install ID when I ran Find-JumpCloudGUID. Both should not be looped together.

# Removing registry keys
$installerKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\$guid"
$classesKey = "HKCR:\Installer\Products\$guid"
$uninstallKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$guid"
$jumpcloudSoftwareKey = "HKLM:\Software\JumpCloud"
$jumpcloudClasses = "HKLM:\SOFTWARE\Classes\Installer\Products\$guid"

$keysToDelete = @($installerKey, $classesKey, $uninstallKey, $jumpcloudSoftwareKey)

foreach ($key in $keysToDelete) {
if (Test-Path $key) {
Remove-Item -Path $key -Recurse -Force -Verbose -ErrorAction SilentlyContinue
} else {
Write-Host "Registry key $key not found."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another instance of MSI installation ID on the $guids variable.
image

}
}

# Stopping and removing the service
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should Stop-Service be before removing reg keys? A running agent might mess with key removals

$serviceName = "jumpcloud-agent"
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was ran twice since I have two $guids values
image

Stop-Service -Name $serviceName -Force -Verbose
start-sleep 6
sc.exe delete $serviceName
start-sleep 6
} else {
Write-Host "Service $serviceName not found."
}

# Removing folder
$jumpcloudFolder = "C:\Program Files\JumpCloud"
if (Test-Path $jumpcloudFolder) {
Remove-Item -Path $jumpcloudFolder -Recurse -ErrorAction SilentlyContinue
}

}
}

# Run the removal function
Remove-JumpCloud