以下是將本地已有的企業微信安裝包安裝到D盤的完整PowerShell腳本,包含詳細的錯誤處理和進度反饋:
<#
.SYNOPSIS使用本地企業微信安裝包安裝到D盤
.DESCRIPTION自動檢測本地安裝包,靜默安裝到指定目錄支持.exe和.msi格式安裝包
#># 強制管理員權限檢查
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {Write-Host "? 必須使用管理員權限運行!右鍵選擇'以管理員身份運行'" -ForegroundColor RedStart-Sleep 3exit 1
}# 配置參數
$InstallPackagePath = "C:\Temp\WeComSetup.exe" # 修改為你的實際安裝包路徑
$InstallPath = "D:\Program Files\WXWork" # 目標安裝路徑# 安裝包存在性檢查
if (-not (Test-Path $InstallPackagePath)) {Write-Host "? 錯誤:安裝包不存在于 $InstallPackagePath" -ForegroundColor RedWrite-Host "請檢查以下可能位置:" -ForegroundColor YellowGet-ChildItem -Path "C:\" -Recurse -Filter "*WeCom*" -Include "*.exe","*.msi" -ErrorAction SilentlyContinue | Select-Object -First 5 -Property FullName | Format-Table -AutoSizeexit 1
}# D盤可用空間檢查(至少需要500MB)
$requiredSpace = 500MB
$diskInfo = Get-PSDrive D -ErrorAction SilentlyContinue
if (-not $diskInfo) {Write-Host "? 錯誤:D盤不可用" -ForegroundColor Redexit 1
}
if ($diskInfo.FreeSpace -lt $requiredSpace) {Write-Host "? 錯誤:D盤空間不足 (需要: $($requiredSpace/1MB) MB, 可用: $($diskInfo.FreeSpace/1MB) MB)" -ForegroundColor Redexit 1
}# 準備安裝目錄
try {if (-not (Test-Path $InstallPath)) {New-Item -Path $InstallPath -ItemType Directory -Force | Out-NullWrite-Host "📁 已創建安裝目錄: $InstallPath" -ForegroundColor Cyan}
} catch {Write-Host "? 目錄創建失敗: $_" -ForegroundColor Redexit 1
}# 檢查是否已安裝相同版本
$uninstallRegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"
$installed = Get-ItemProperty $uninstallRegPath -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -like "*企業微信*" -and $_.InstallLocation -like "$InstallPath*" }if ($installed) {Write-Host "?? 已檢測到已安裝版本: $($installed.DisplayVersion)" -ForegroundColor Yellow$choice = Read-Host "是否重新安裝? (Y/N)"if ($choice -ne 'Y') { exit }
}# 根據安裝包類型執行安裝
$fileExt = [System.IO.Path]::GetExtension($InstallPackagePath).ToLower()try {Write-Host "🛠? 正在安裝到 $InstallPath ..." -ForegroundColor Cyanif ($fileExt -eq ".exe") {# EXE安裝包參數(企業微信專用靜默參數)$installArgs = "/S /D=$InstallPath"$process = Start-Process -FilePath $InstallPackagePath -ArgumentList $installArgs -Wait -PassThru# 驗證安裝結果if ($process.ExitCode -ne 0) {throw "安裝程序返回錯誤代碼: $($process.ExitCode)"}}elseif ($fileExt -eq ".msi") {# MSI安裝包參數$installArgs = "/i `"$InstallPackagePath`" INSTALLDIR=`"$InstallPath`" /qn"$process = Start-Process "msiexec.exe" -ArgumentList $installArgs -Wait -PassThruif ($process.ExitCode -ne 0) {throw "MSI安裝失敗,錯誤代碼: $($process.ExitCode)"}}else {throw "不支持的安裝包格式: $fileExt"}# 驗證主程序是否存在if (-not (Test-Path "$InstallPath\WXWork.exe")) {throw "主程序文件未找到,可能安裝不完整"}Write-Host "? 安裝成功!" -ForegroundColor Green# 創建開始菜單快捷方式(可選)$startMenuPath = "$env:ProgramData\Microsoft\Windows\Start Menu\Programs"$shortcutPath = "$startMenuPath\企業微信.lnk"$shell = New-Object -ComObject WScript.Shell$shortcut = $shell.CreateShortcut($shortcutPath)$shortcut.TargetPath = "$InstallPath\WXWork.exe"$shortcut.WorkingDirectory = $InstallPath$shortcut.Save()} catch {Write-Host "? 安裝失敗: $_" -ForegroundColor Redexit 1
}# 添加環境變量(可選)
try {$envPath = [Environment]::GetEnvironmentVariable("Path", "Machine")if (-not $envPath.Contains($InstallPath)) {[Environment]::SetEnvironmentVariable("Path", $envPath + ";$InstallPath", "Machine")Write-Host "🔧 已添加環境變量" -ForegroundColor Cyan}
} catch {Write-Host "?? 環境變量設置失敗: $_" -ForegroundColor Yellow
}Write-Host "🎉 企業微信已成功安裝到: $InstallPath" -ForegroundColor Magenta
Write-Host "👉 主程序路徑: $InstallPath\WXWork.exe" -ForegroundColor Cyan