Run MsiExec from PowerShell and get Return Code

前端 未结 3 1387
庸人自扰
庸人自扰 2020-12-25 11:44

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

相关标签:
3条回答
  • 2020-12-25 12:19

    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/

    0 讨论(0)
  • 2020-12-25 12:21

    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
    
    0 讨论(0)
  • 2020-12-25 12:39
    $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
    
    0 讨论(0)
提交回复
热议问题