![我的脚本如何确定某个 Windows Installer 更新 (MSP) 的安装是否成功或失败?](https://linux22.com/image/1531293/%E6%88%91%E7%9A%84%E8%84%9A%E6%9C%AC%E5%A6%82%E4%BD%95%E7%A1%AE%E5%AE%9A%E6%9F%90%E4%B8%AA%20Windows%20Installer%20%E6%9B%B4%E6%96%B0%20(MSP)%20%E7%9A%84%E5%AE%89%E8%A3%85%E6%98%AF%E5%90%A6%E6%88%90%E5%8A%9F%E6%88%96%E5%A4%B1%E8%B4%A5%EF%BC%9F.png)
我编写了一个 PowerShell 脚本(见下文)来依次安装大量 MSP 更新(带.msp
扩展名的文件,通过 Windows Installer 部署)。现在,我希望此脚本还能告诉我 MSP 更新安装何时失败。
我尝试过的事情:查询错误代码。有两种方法:
- 一种是运行后直接用$LASTEXITCODE获取错误代码
MSIEXEC.EXE
,比较繁琐。 另一个涉及将
-PassThru
开关添加到Start-Process
,将其结果存储到对象中,例如,$a
使用读取错误代码$a.ExitCode
。像这样:$a=Start-Process msiexec.exe -ArgumentList "/p `"$MspRelPath`" /log `"$LogRelPath`" /passive /norestart" -Wait -PassThru Write-Host $a.ExitCode
两者都没有用。它似乎msiexec.exe
总是返回零作为退出代码。
如果有人感兴趣的话,下面是脚本:
param (
[parameter(mandatory=$false)][Switch]$BypassAdminPrompt
)
Try
{
Clear-Host
# Get script name
$ScriptFileObject=(Get-Item $PSCommandPath)
$ScriptName=$ScriptFileObject.Name
$ScriptPath=$ScriptFileObject.DirectoryName
# Load Windows Forms and initialize visual styles
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles()
# Is the script holding administrative privileges?
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if ($IsAdmin -eq $false) {
if (!$BypassAdminPrompt) {
Start-Process powershell.exe -ArgumentList "-ExecutionPolicy $env:PSExecutionPolicyPreference -File `"$PSCommandPath`" -BypassAdminPrompt" -Verb RunAs
} else {
$result=[System.Windows.Forms.MessageBox]::Show("This script requires administrative privileges, which are absent.", $ScriptName, "OK", "Error");
}
break;
}
# Install...
Set-Location $ScriptPath
$MSP_list = Get-ChildItem *.msp -Recurse
if ($MSP_list -eq $null) {
$result=[System.Windows.Forms.MessageBox]::Show("Nothing found to install.`rSearch path was "+$ScriptPath, $ScriptName, "OK", "Error");
}
else
{
$MSP_list | ForEach-Object {
# Ordinarily, I'd pass the path in the form of ".\foldername\filename.msp" but Windows Installer does not accept that.
# It must be in "foldername\filename.msp" form.
$MspRelPath = $_.FullName.Substring($ScriptPath.Length+1)
$LogRelPath = $MspRelPath+".log"
Write-Host $MspRelPath
Start-Process msiexec.exe -ArgumentList "/p `"$MspRelPath`" /log `"$LogRelPath`" /passive /norestart" -Wait
}
Remove-Variable MspRelPath
Remove-Variable LogRelPath
Pause
}
Remove-Variable MSP_list
}
Catch
{
$result=[System.Windows.Forms.MessageBox]::Show("Error!`r`r"+$Error[0], $ScriptName, "OK", "Error");
break;
}
答案1
检查 Windows 事件中的 MSIExec 事件或完成后获取日志输出内容并检查故障指示。
我有一个使用 MSIExec 远程安装 MSI 的脚本,等待 MSIExec 关闭(或进程/服务启动)...如果在典型安装时间后没有任何反应,我会检查 MSIExec 调用中包含的日志路径并检查失败或成功消息