Run MsiExec from PowerShell and get Return Code

99封情书 提交于 2019-12-03 05:38:06

问题


With BAT/CMD script I can simply use "msiexec /i <whatever.msi> /quiet /norestart" and then check %errorlevel% for the result.

With VBScript, using the Wscript.Shell object Run() method, I can get the result like this:

"result = oShell.Run("msiexec /i ...", 1, True)"

How can I do this with PowerShell?


回答1:


I would wrap that up in Start-Process and use the ExitCode property of the resulting process object. For example

(Start-Process -FilePath "msiexec.exe" -ArgumentList "<<whatever>>" -Wait -Passthru).ExitCode



回答2:


$LastExitCode

or

$?

depending on what you're after. The former is an integer, the latter just a boolean. Furthermore, $LastExitCode is only populated for native programs being run, while $? generally tells whether the last command run was successful or not – so it will also be set for cmdlets.

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



回答3:


You can also use the powershell app deployment kit which provides several things.

Then you can use for example

Execute-MSI -Action 'Install' -Path "$dirFiles\your.msi" -AddParameters "INSTALLFOLDER=C:\$appFolder"

info http://psappdeploytoolkit.com/



来源:https://stackoverflow.com/questions/4124409/run-msiexec-from-powershell-and-get-return-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!