スクリプトで、特定の Windows インストーラー更新プログラム (MSP) のインストールが成功したか失敗したかを判断するにはどうすればよいでしょうか?

スクリプトで、特定の Windows インストーラー更新プログラム (MSP) のインストールが成功したか失敗したかを判断するにはどうすればよいでしょうか?

非常に多くの MSP 更新 ( .mspWindows インストーラー経由で展開された拡張子を持つファイル) を次々にインストールするための PowerShell スクリプト (下記参照) を作成しました。ここで、このスクリプトで MSP 更新のインストールが失敗したときにも通知するようにしたいと思います。

私が試したこと:エラーコードのクエリ。アプローチは 2 つあります。

  • 1つは、直接実行した後に $LASTEXITCODE を使用してエラー コードを取得することですMSIEXEC.EXE。これは面倒です。
  • もう 1 つは、-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

MSIExec イベントの Windows イベントを確認するか、完了したらログ出力の内容を取得して、失敗の兆候がないか確認します。

MSIExec を使用してリモートで MSI をインストールし、MSIExec が終了する (またはプロセス/サービスが開始する) のを待つスクリプトがあります...通常のインストール時間後に何も起こらない場合は、MSIExec 呼び出しに含まれるログ パスをチェックし、失敗または成功のメッセージを確認します。

関連情報