%20%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%8C%E6%88%90%E5%8A%9F%E3%81%97%E3%81%9F%E3%81%8B%E5%A4%B1%E6%95%97%E3%81%97%E3%81%9F%E3%81%8B%E3%82%92%E5%88%A4%E6%96%AD%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%97%E3%82%87%E3%81%86%E3%81%8B%3F.png)
非常に多くの MSP 更新 ( .msp
Windows インストーラー経由で展開された拡張子を持つファイル) を次々にインストールするための 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 呼び出しに含まれるログ パスをチェックし、失敗または成功のメッセージを確認します。