Find exit code for executing a cmd command through PowerShell

前端 未结 3 1916
再見小時候
再見小時候 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
    

提交回复
热议问题