Files
win-remote-toolkit/scripts/powershell/install-cports.ps1
2026-04-24 17:44:02 +08:00

67 lines
2.5 KiB
PowerShell

[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = 'Stop'
$url = 'https://www.nirsoft.net/utils/cports-x64.zip'
$installDir = 'C:\Program Files\NirSoft\CurrPorts'
$exeName = 'cports.exe'
$exePath = Join-Path $installDir $exeName
# Idempotent
if (Test-Path $exePath) {
$ver = (Get-Item $exePath).VersionInfo.ProductVersion
Write-Host "[=] CurrPorts already installed: $exePath (v$ver)"
} else {
$tmpZip = Join-Path $env:TEMP 'cports-x64.zip'
$tmpDir = Join-Path $env:TEMP 'cports-extract'
if (Test-Path $tmpDir) { Remove-Item $tmpDir -Recurse -Force }
New-Item -ItemType Directory -Path $tmpDir | Out-Null
Write-Host "[*] Downloading $url"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $url -OutFile $tmpZip -UseBasicParsing
if ((Get-Item $tmpZip).Length -lt 50KB) { throw "Downloaded zip too small" }
Expand-Archive -Path $tmpZip -DestinationPath $tmpDir -Force
if (-not (Test-Path (Join-Path $tmpDir $exeName))) {
throw "$exeName not found in archive"
}
New-Item -ItemType Directory -Force -Path $installDir | Out-Null
Copy-Item -Path (Join-Path $tmpDir '*') -Destination $installDir -Recurse -Force
Remove-Item $tmpZip -Force -ErrorAction SilentlyContinue
Remove-Item $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
$ver = (Get-Item $exePath).VersionInfo.ProductVersion
Write-Host "[OK] Installed: $exePath (v$ver)"
}
# Start Menu shortcut for all users (Common Start Menu)
$commonStart = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'
$lnkCommon = Join-Path $commonStart 'CurrPorts.lnk'
$wsh = New-Object -ComObject WScript.Shell
$lnk = $wsh.CreateShortcut($lnkCommon)
$lnk.TargetPath = $exePath
$lnk.WorkingDirectory = $installDir
$lnk.IconLocation = "$exePath,0"
$lnk.Description = 'NirSoft CurrPorts - list open TCP/UDP ports'
$lnk.Save()
Write-Host "[*] Start Menu (All Users) shortcut: $lnkCommon"
# Also desktop shortcut for the 'User' account (main daily-use account)
$userDesktop = 'C:\Users\user\Desktop'
if (Test-Path $userDesktop) {
$lnkUser = Join-Path $userDesktop 'CurrPorts.lnk'
$lnk2 = $wsh.CreateShortcut($lnkUser)
$lnk2.TargetPath = $exePath
$lnk2.WorkingDirectory = $installDir
$lnk2.IconLocation = "$exePath,0"
$lnk2.Save()
& icacls $lnkUser /grant 'User:F' /Q | Out-Null
Write-Host "[*] Desktop shortcut for User: $lnkUser"
}
Write-Host ""
Write-Host "Launch from Start menu, or run directly:"
Write-Host " `"$exePath`""