从PowerShell运行MSIEXEC并获取返回代码
-
29-09-2019 - |
题
和 BAT/CMD
我可以简单地使用脚本 "msiexec /i <whatever.msi> /quiet /norestart"
然后检查 %errorlevel%
为结果。
和 VBScript
, , 使用 Wscript.Shell
目的 Run()
方法,我可以得到这样的结果:
"result = oShell.Run("msiexec /i ...", 1, True)"
我该如何使用PowerShell?
解决方案
我将其包装在开始过程中,并使用所得过程对象的ExitCode属性。例如
(Start-Process -FilePath "msiexec.exe" -ArgumentList "<<whatever>>" -Wait -Passthru).ExitCode
其他提示
$LastExitCode
或者
$?
取决于您所追求的。前者是整数,后者只是布尔人。此外, $LastExitCode
仅针对正在运行的本地程序填充 $?
通常说明最后一个命令运行是否成功 - 因此它也将为CMDLET设置。
PS Home:\> cmd /c "echo foo"; $?,$LASTEXITCODE
foo
True
0
PS Home:\> cmd /c "ech foo"; $?,$LASTEXITCODE
'ech' is not recognized as an internal or external command,
operable program or batch file.
False
1
您还可以使用提供几件事的PowerShell应用程序部署套件。
然后您可以使用
Execute-MSI -Action 'Install' -Path "$dirFiles\your.msi" -AddParameters "INSTALLFOLDER=C:\$appFolder"
不隶属于 StackOverflow