You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过PowerShell部署MSI医疗应用遇问题求助

医疗应用批量部署PowerShell脚本问题修复

问题场景

为15台管控严格的工作站批量部署医疗应用,流程要求:

  • 停止目标应用进程
  • 卸载旧版本
  • 安装新版本

核心报错

  1. 卸载异常:脚本执行时提示「应用未安装或不是有效的64位应用」,但手动执行卸载字符串可正常完成,卸载日志无有效报错信息
  2. 安装失败:手动点击带管理员权限标识的安装按钮可正常完成,自动化执行时触发MSI Error 1603

用户提供的原始脚本

$SourcePath = "LOCATION OF Source"
$Destination = "C:\Temp"
$App = Get-WmiObject win32_product | where{$_.name -like "mammoscreen*"}
$Install = 'C:\Temp\tpx-client-v2.2.3.msi'

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path "C:\Temp\TheraInstall.log" -append

#Do some stuff and begin logging
Write-Output "Start Logging"
Get-Date

Write-Host "Copying files..." -ForegroundColor Green
Copy-Item $SourcePath -Destination $Destination

Write-Host "Stopping process..." -ForegroundColor Cyan
Stop-Process -ProcessName tpx-client-agent

#Uninstall old versions of software
Write-Host "Uninstalling previous versions..." -ForegroundColor yellow
Start-Process 'C:\Windows\System32\msiexec.exe' -ArgumentList "/X $($App.IdentifyingNumber)/qn /norestart /L*v C:\Temp\UninstallTest.log" -Wait


#Installs the new Client
Write-Host "Installing new agent v2.2.3..." -ForegroundColor Green
Start-Process 'C:\Windows\System32\msiexec.exe' -Wait -ArgumentList '/I $Install /qn /norestart /L*v C:\Temp\InstallTest.log'

#Stop logging
Stop-Transcript

#Start-Process -Filepath "tpx-client-agent"

针对性修复方案

1. 替换不可靠的卸载检测逻辑

Get-WmiObject win32_product会触发MSI自动修复,在管控环境中易引发异常,且对32/64位应用的识别存在偏差。改用注册表读取方式获取卸载信息:

# 同时扫描64位和32位应用的卸载注册表项
$uninstallPaths = @(
    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
    'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
$App = Get-ItemProperty -Path $uninstallPaths -ErrorAction SilentlyContinue | 
       Where-Object { $_.DisplayName -like 'mammoscreen*' -and $_.UninstallString }

执行卸载时直接使用注册表中的卸载字符串,避免参数拼接错误:

if ($App) {
    Write-Host "Uninstalling previous versions..." -ForegroundColor Yellow
    # 清理卸载字符串中的冗余内容,保留核心参数
    $uninstallArgs = $App.UninstallString -replace 'msiexec.exe', '' -replace '"', ''
    # 添加静默卸载参数
    $uninstallArgs += ' /qn /norestart /L*v C:\Temp\UninstallTest.log'
    Start-Process 'msiexec.exe' -ArgumentList $uninstallArgs -Wait -NoNewWindow
} else {
    Write-Host "未找到旧版本应用" -ForegroundColor Yellow
}

2. 修复安装命令的变量解析错误

原脚本中$Install变量在单引号参数中无法被解析,导致msiexec无法定位安装包。改用数组传递参数(最可靠的方式):

Write-Host "Installing new agent v2.2.3..." -ForegroundColor Green
$installArgs = @(
    '/I', $Install,
    '/qn',
    '/norestart',
    '/L*v', 'C:\Temp\InstallTest.log'
)
Start-Process 'msiexec.exe' -Wait -ArgumentList $installArgs -NoNewWindow -ErrorAction Stop

3. 强化进程停止的鲁棒性

原Stop-Process未处理进程不存在的情况,且未强制终止可能残留的进程:

Write-Host "Stopping process..." -ForegroundColor Cyan
$process = Get-Process -Name 'tpx-client-agent' -ErrorAction SilentlyContinue
if ($process) {
    Stop-Process -Id $process.Id -Force -ErrorAction Stop
    Write-Host "进程已终止" -ForegroundColor Green
} else {
    Write-Host "未找到目标进程" -ForegroundColor Yellow
}

4. 添加管理员权限检测

管控环境中必须确保脚本以管理员权限执行,添加前置检测:

# 检测当前会话是否为管理员权限
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Error "脚本需要管理员权限执行,请右键选择「以管理员身份运行」"
    exit 1
}

5. 完善错误捕获与日志

在关键步骤添加错误捕获,避免脚本静默失败:

Write-Host "Copying files..." -ForegroundColor Green
try {
    Copy-Item $SourcePath -Destination $Destination -Force -ErrorAction Stop
    Write-Host "文件复制完成" -ForegroundColor Green
} catch {
    Write-Error "文件复制失败:$($_.Exception.Message)"
    Stop-Transcript
    exit 1
}

修复后的完整脚本示例

# 管理员权限检测
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Error "脚本需要管理员权限执行,请右键选择「以管理员身份运行」"
    exit 1
}

$SourcePath = "LOCATION OF Source"
$Destination = "C:\Temp"
$Install = 'C:\Temp\tpx-client-v2.2.3.msi'

# 初始化日志
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path "C:\Temp\TheraInstall.log" -append

Write-Output "Start Logging"
Get-Date

# 复制安装文件
Write-Host "Copying files..." -ForegroundColor Green
try {
    Copy-Item $SourcePath -Destination $Destination -Force -ErrorAction Stop
    Write-Host "文件复制完成" -ForegroundColor Green
} catch {
    Write-Error "文件复制失败:$($_.Exception.Message)"
    Stop-Transcript
    exit 1
}

# 停止应用进程
Write-Host "Stopping process..." -ForegroundColor Cyan
$process = Get-Process -Name 'tpx-client-agent' -ErrorAction SilentlyContinue
if ($process) {
    Stop-Process -Id $process.Id -Force -ErrorAction Stop
    Write-Host "进程已终止" -ForegroundColor Green
} else {
    Write-Host "未找到目标进程" -ForegroundColor Yellow
}

# 卸载旧版本
$uninstallPaths = @(
    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
    'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
$App = Get-ItemProperty -Path $uninstallPaths -ErrorAction SilentlyContinue | 
       Where-Object { $_.DisplayName -like 'mammoscreen*' -and $_.UninstallString }

if ($App) {
    Write-Host "Uninstalling previous versions..." -ForegroundColor Yellow
    $uninstallArgs = $App.UninstallString -replace 'msiexec.exe', '' -replace '"', ''
    $uninstallArgs += ' /qn /norestart /L*v C:\Temp\UninstallTest.log'
    Start-Process 'msiexec.exe' -ArgumentList $uninstallArgs -Wait -NoNewWindow
} else {
    Write-Host "未找到旧版本应用" -ForegroundColor Yellow
}

# 安装新版本
Write-Host "Installing new agent v2.2.3..." -ForegroundColor Green
try {
    $installArgs = @(
        '/I', $Install,
        '/qn',
        '/norestart',
        '/L*v', 'C:\Temp\InstallTest.log'
    )
    Start-Process 'msiexec.exe' -Wait -ArgumentList $installArgs -NoNewWindow -ErrorAction Stop
    Write-Host "新版本安装完成" -ForegroundColor Green
} catch {
    Write-Error "安装失败:$($_.Exception.Message)"
    Stop-Transcript
    exit 1
}

# 停止日志
Stop-Transcript

内容的提问来源于stack exchange,提问作者WRCKid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:40:34