[英]Install a .msi file and display the exit code of the process(.msi file)
我想安裝一個.msi 文件(它是軟件winrar)並顯示進程的退出代碼(.msi 文件)。 但我得到這個錯誤:
PS D:\powershell> d:\powershell\boucle.ps1
InvalidOperation: D:\powershell\boucle.ps1:20:1
Line |
20 | $p.WaitForExit()
| ~~~~~~~~~~~~~~~~
| You cannot call a method on a null-valued expression.
然后,我無法安裝.msi 文件。 這是安裝 a.msi 文件的好方法嗎? 請問你有解決辦法嗎?
這是我的代碼:
function executemsifile {
param( [string]$msiFile )
$arguments = @(
"/i"
"`"$msiFile`""
"/q"
"/l*vx"
"log.txt"
)
$p = Start-Process -FilePath msiexec.exe -ArgumentList $arguments
$p.WaitForExit()
$p.ExitCode
Write-Host $p.ExitCode
}
executemsifile "D:\update4\wrar_600_64bit.msi"
您缺少Start-Process
的-PassThru
參數,因此您不會收到 output 並且$p
將為$null
。
正如mklement0指出的那樣,一種可能的簡化是還使用-Wait
開關而不是單獨的$p.WaitForExit()
調用。
$p=Start-Process -FilePath msiexec.exe -ArgumentList $arguments -Wait -PassThru
$p.ExitCode
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.