脚本示例
# Uninstall-CLI-Windows.ps1
# Purpose: Automatically detect and uninstall Aliyun CLI, and delete configuration files in user directory
[CmdletBinding()]
param (
[switch]$Clean,
[switch]$Help
)
function Show-Usage {
Write-Output @"
Alibaba Cloud Command Line Interface Uninstaller
-Help Display this help and exit
-Clean Remove user config file
"@
}
function Remove-DirectoryIfEmpty {
param([string]$Path)
if ((Get-ChildItem -Path $Path -Force).Count -eq 0) {
Remove-Item -Path $Path -Force
}
}
function Remove-AliyunCLIFromPath {
param([string]$PathToRemove)
$Key = 'HKCU:\Environment'
$CurrentPath = (Get-ItemProperty -Path $Key -Name PATH).PATH
if ($CurrentPath -like "*$PathToRemove*") {
$newPath = ($CurrentPath -split ';' | Where-Object { $_ -ne $PathToRemove }) -join ';'
Set-ItemProperty -Path $Key -Name PATH -Value $newPath
$env:PATH = $newPath
}
}
function Remove-AliyunCLI {
$AliyunBinary = (Get-Command aliyun -ErrorAction SilentlyContinue).Source
if ($AliyunBinary -and (Test-Path $AliyunBinary)) {
Remove-Item -Path $AliyunBinary -Force
$AliyunInstallDir = Split-Path -Parent $AliyunBinary
Remove-DirectoryIfEmpty -Path $AliyunInstallDir
Remove-AliyunCLIFromPath -PathToRemove $AliyunInstallDir
Write-Output "Aliyun CLI binary has been removed."
}
}
function Remove-ConfigFile {
$ConfigDir = Join-Path $HOME ".aliyun"
$ConfigFile = Join-Path $ConfigDir "config.json"
if (Test-Path $ConfigFile) {
Remove-Item -Path $ConfigFile -Force
Remove-DirectoryIfEmpty -Path $ConfigDir
Write-Output "Aliyun CLI config file has been removed."
}
}
if ($PSBoundParameters['Help']) {
Show-Usage
exit 0
}
Write-Output @"
..............888888888888888888888 ........=8888888888888888888D=..............
...........88888888888888888888888 ..........D8888888888888888888888I...........
.........,8888888888888ZI: ...........................=Z88D8888888888D..........
.........+88888888 ..........................................88888888D..........
.........+88888888 .......Welcome to use Alibaba Cloud.......O8888888D..........
.........+88888888 ............. ************* ..............O8888888D..........
.........+88888888 .... Command Line Interface(Reloaded) ....O8888888D..........
.........+88888888...........................................88888888D..........
..........D888888888888DO+. ..........................?ND888888888888D..........
...........O8888888888888888888888...........D8888888888888888888888=...........
............ .:D8888888888888888888.........78888888888888888888O ..............
"@
try {
Remove-AliyunCLI
if ($PSBoundParameters['Clean']) { Remove-ConfigFile }
Write-Output "Aliyun CLI has been uninstalled."
} catch {
Write-Output "Failed to uninstall Aliyun CLI: $_"
}