当前位置: 首页 > news >正文

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

以下是将本地已有的企业微信安装包安装到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
http://www.lryc.cn/news/613453.html

相关文章:

  • VSCode ssh一直在Setting up SSH Host xxx: Copying VS Code Server to host with scp等待
  • 开发避坑指南(20) :MyBatis操作Oracle插入NULL值异常“无效列类型1111“解决方案
  • DrissionPage实战案例:小红书旅游数据爬取
  • TDengine IDMP 文档介绍
  • 腾讯位置服务 —— 预估订单路线金额(使用Drools规则引擎处理)
  • 机器学习在量化中的应用:如何从逻辑回归到XGBoost实现高效预测?
  • [Oracle] DECODE()函数
  • DBeaver 25.1.0 转储数据库失败解决方案(适配最新版界面)
  • [Oracle] GREATEST()函数
  • 数据库入门:从零开始构建你的第一个数据库
  • 一个基于固定 IP地址查询天气的 C 语言程序,通过调用第三方天气 API:
  • Oracle 关闭 impdp任务
  • Oracle 12c + Pl/Sql windows系统下表空间创建、迁移,dmp备份导入,数据库字符集更改
  • 图论(1):图数据结构
  • 攻防世界WEB(新手模式)2-2-upload1
  • 【YOLO学习笔记】YOLOv8详解解读
  • 工业相机使用 YOLOv8深度学习模型 及 OpenCV 实现目标检测简单介绍
  • Moses工具的配置和小语种平行语料训练SMT完整实现
  • 商城小程序怎么做?如何开发母婴用品商城小程序?
  • 前端三大核心要素以及前后端通讯
  • mysql_mcp_server_pro源码部署及启动报错新手指南:让智能体长出手来直接获取到最底层的数据
  • Linux ISCSI服务配置
  • 聚集索引VS非聚集索引:核心差异详解
  • 将Excel数据导入SQL Server数据库,并更新源表数据
  • 安卓Handler和Looper的学习记录
  • ArkTS: McPointChart
  • Debian系统 为账号添加sudo权限
  • 远程制作《最后生还者》中的Xsens动作捕捉技术
  • Maven分模块开发实战指南
  • Windows下安装SageAttention