windows有一个企业微信安装包,脚本执行并安装到d盘。

发布于:2025-08-09 ⋅ 阅读:(19) ⋅ 点赞:(0)

以下是将本地已有的企业微信安装包安装到D盘的完整PowerShell脚本,包含详细的错误处理和进度反馈:

<#
.SYNOPSIS
    使用本地企业微信安装包安装到D盘
.DESCRIPTION
    自动检测本地安装包,静默安装到指定目录
    支持.exe和.msi格式安装包
#>

# 强制管理员权限检查
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "❗ 必须使用管理员权限运行!右键选择'以管理员身份运行'" -ForegroundColor Red
    Start-Sleep 3
    exit 1
}

# 配置参数
$InstallPackagePath = "C:\Temp\WeComSetup.exe"  # 修改为你的实际安装包路径
$InstallPath = "D:\Program Files\WXWork"       # 目标安装路径

# 安装包存在性检查
if (-not (Test-Path $InstallPackagePath)) {
    Write-Host "❌ 错误:安装包不存在于 $InstallPackagePath" -ForegroundColor Red
    Write-Host "请检查以下可能位置:" -ForegroundColor Yellow
    Get-ChildItem -Path "C:\" -Recurse -Filter "*WeCom*" -Include "*.exe","*.msi" -ErrorAction SilentlyContinue | 
        Select-Object -First 5 -Property FullName | Format-Table -AutoSize
    exit 1
}

# D盘可用空间检查(至少需要500MB)
$requiredSpace = 500MB
$diskInfo = Get-PSDrive D -ErrorAction SilentlyContinue
if (-not $diskInfo) {
    Write-Host "❌ 错误:D盘不可用" -ForegroundColor Red
    exit 1
}
if ($diskInfo.FreeSpace -lt $requiredSpace) {
    Write-Host "❌ 错误:D盘空间不足 (需要: $($requiredSpace/1MB) MB, 可用: $($diskInfo.FreeSpace/1MB) MB)" -ForegroundColor Red
    exit 1
}

# 准备安装目录
try {
    if (-not (Test-Path $InstallPath)) {
        New-Item -Path $InstallPath -ItemType Directory -Force | Out-Null
        Write-Host "📁 已创建安装目录: $InstallPath" -ForegroundColor Cyan
    }
} catch {
    Write-Host "❌ 目录创建失败: $_" -ForegroundColor Red
    exit 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 Cyan
    
    if ($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 -PassThru
        
        if ($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 Red
    exit 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

网站公告

今日签到

点亮在社区的每一天
去签到