Obtaining ExitCode using Start-Process and WaitForExit instead of -Wait

后端 未结 6 1028
一生所求
一生所求 2020-11-29 01:29

I\'m trying to run a program from PowerShell, wait for the exit, then get access to the ExitCode, but I am not having much luck. I don\'t want to use -Wait with

6条回答
  •  被撕碎了的回忆
    2020-11-29 02:24

    There are two things to remember here. One is to add the -PassThru argument and two is to add the -Wait argument. You need to add the wait argument because of this defect: http://connect.microsoft.com/PowerShell/feedback/details/520554/start-process-does-not-return-exitcode-property

    -PassThru []
        Returns a process object for each process that the cmdlet started. By d
        efault, this cmdlet does not generate any output.
    

    Once you do this a process object is passed back and you can look at the ExitCode property of that object. Here is an example:

    $process = start-process ping.exe -windowstyle Hidden -ArgumentList "-n 1 -w 127.0.0.1" -PassThru -Wait
    $process.ExitCode
    
    # This will print 1
    

    If you run it without -PassThru or -Wait, it will print out nothing.

    The same answer is here: How do I run a Windows installer and get a succeed/fail value in PowerShell?

提交回复
热议问题