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

AzureAuth Windows uninstall script #340

Merged
merged 15 commits into from
Oct 12, 2023
Merged
Changes from 4 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 install/uninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Enable a default -Verbose flag for debug output.
[CmdletBinding()]

# Halt script execution at the first failed command.
$script:ErrorActionPreference='Stop'

$deleted = $false
reillysiemens marked this conversation as resolved.
Show resolved Hide resolved

function Uninstall {
$uninstallDirectory = Get-UninstallDirectory
Remove-InstallationFolder $uninstallDirectory
Remove-FromPath $uninstallDirectory

if ($deleted) {
Write-Output "Uninstalled AzureAuth!"
} else {
Write-Output "There were no AzureAuth installations to be deleted."
reillysiemens marked this conversation as resolved.
Show resolved Hide resolved
}
}

function Get-UninstallDirectory {
$directory = if (![string]::IsNullOrEmpty($Env:AZUREAUTH_INSTALL_DIRECTORY)) {
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
Write-Error "Uninstalling from custom location is not supported"
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
}
return ([System.IO.Path]::Combine($Env:LOCALAPPDATA, "Programs", "AzureAuth"))
}

function Remove-InstallationFolder {
param ([string]$directory)

if (Test-Path -Path $directory) {
Write-Verbose "Removing installations at '${directory}'"
Remove-Item -Force -Recurse $directory
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
$script:deleted = $true
} else {
Write-Verbose "There were no installations found at '${directory}'"
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
}
}

function Remove-FromPath {
param ([string]$uninstallDirectory)

$registryPath = 'Registry::HKEY_CURRENT_USER\Environment'
$currentPath = (Get-ItemProperty -Path $registryPath -Name PATH -ErrorAction SilentlyContinue).Path

# We try to find any other AzureAuth installations that are not in default location.
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
# Installations in custom locations that are not listed in PATH cannot be found.
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
$azureauthsInPath = (Get-Command -Name azureauth -ErrorAction SilentlyContinue -CommandType Application -All).Source
$otherDirectories = [System.Collections.ArrayList]@()
ForEach($az in $azureauthsInPath) {
if (!$az.Contains($uninstallDirectory)) {
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
$additionalDirectory = (Get-Item $az).Directory.FullName
Write-Verbose "Additional installation found in ${additionalDirectory}"
$_ = $otherDirectories.Add($additionalDirectory)
Remove-InstallationFolder ($additionalDirectory)
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
}
}

# Reconstruct the $PATH without any azureauth directories.
$updatedPath = "";
if (($null) -ne $currentPath) {
$paths = $currentPath.Split(";")
$pathArr = @()
ForEach($path in $paths){
if(!$path.Equals("") -And !$path.Contains($uninstallDirectory) -And `
!$otherDirectories.Contains($path)) {
$pathArr += "${path}"
}
elseif (!$path.Equals("")) {
Write-Verbose "Removing '${path}' from `$env:PATH"
}
}
$updatedPath = ($pathArr -join ";") + ";"
}

Set-ItemProperty -Path $registryPath -Name PATH -Value $updatedPath
Send-SettingChange
Write-Verbose "Custom installations that were not in `$env:Path could not be deleted."
Emilio0404 marked this conversation as resolved.
Show resolved Hide resolved
}

# Send WM_SETTINGCHANGE after changing Environment variables.
# Refer to https://gist.github.com/alphp/78fffb6d69e5bb863c76bbfc767effda
function Send-SettingChange {
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
$HWND_BROADCAST = [IntPtr] 0xffff;
$WM_SETTINGCHANGE = 0x1a;
$result = [UIntPtr]::Zero

[void] ([Win32.Nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, "Environment", 2, 5000, [ref] $result))
}

Uninstall