Find exit code for executing a cmd command through PowerShell

前端 未结 3 1912
再見小時候
再見小時候 2020-12-20 16:33

I am using a silent installation command to install software. I am running this command from PowerShell 3.0.

$silentInstall = C:\\Users\\Admin\\Documents\\Se         


        
相关标签:
3条回答
  • 2020-12-20 16:50

    It looks like you're running an MSI installer. When running from the console, control is immediately returned while MSI forks a new process to run the installer. There is no way to change this behavior.

    What you'll probably need to do is use Get-Process to find a process named msiexec, and wait for it to finish. There is always an msiexec process running, which handles starting new installers, so you'll need to find the msiexec process that started after your install began.

    $msiexecd = Get-Process -Name 'msiexec'
    C:\Users\Admin\Documents\Setup-2.0.exe exe `
                                           /s `
                                           /v"EULAACCEPTED=\"Yes\" /l*v c:\install.log /qn"
    $myMsi = Get-Process -Name 'msiexec' | 
                 Where-Object { $_.Id -ne $msiexecd.Id }
    $myMsi.WaitForExit()
    Write-Verbose $myMsi.ExitCode
    
    0 讨论(0)
  • 2020-12-20 16:53

    It depends on how the EXE file runs - sometimes it will kick off a separate process and return immediately, and in such cases this usually works -

    $p = Start-Process -FilePath <path> -ArgumentList <args> -Wait -NoNewWindow -PassThru
    $p.ExitCode
    

    Otherwise this usually works -

    & <path> <args>
    $LASTEXITCODE
    

    Or sometimes this -

    & cmd.exe /c <path> <args>
    $LASTEXITCODE
    
    0 讨论(0)
  • 2020-12-20 17:04

    You shouldn't need to use Invoke-Expression:

    & C:\Users\Admin\Documents\Setup-2.0.exe /s /vEULAACCEPTED=Yes /l*v C:\install.log /qn
    
    0 讨论(0)
提交回复
热议问题