我的腳本如何確定某個 Windows Installer 更新 (MSP) 的安裝是成功還是失敗?

我的腳本如何確定某個 Windows Installer 更新 (MSP) 的安裝是成功還是失敗?

我編寫了一個 PowerShell 腳本(見下文)來逐一安裝大量 MSP 更新(帶有.msp擴展名的文件,透過 Windows Installer 部署)。現在,我希望此腳本也能在 MSP 更新安裝失敗時告訴我。

我嘗試過的事情:查詢錯誤代碼。有兩種方法:

  • 一種是直接運作後使用$LASTEXITCODE取得錯誤碼MSIEXEC.EXE。這很乏味。
  • 另一個涉及添加-PassThruswitch 到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 呼叫中包含的日誌路徑並檢查失敗或成功訊息

相關內容