Powershell waitforexit example

Powershell waitforexit example

Author: Creedderign On: 30.05.2017

I'm trying to run a program from PowerShell, wait for the exit, then get access to the ExitCode, but not having much luck.

powershell waitforexit example

I don't want to use -Wait with Start-Process, as I need some processing to carry on in the background. Running this script will cause notepad to be started. After this is closed manually, the exit code will be printed, and it will start again, without using -wait. No ExitCode is provided when this is quit:. I need to be able to perform additional processing between starting the program and waiting for it to quit, so I can't make use of -Wait.

Any idea how I can do this and still have access to the. ExitCode property from this process? 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: 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:.

While trying out the final suggestion above, I discovered an even simpler solution. All I had to do was cache the process handle.

powershell - Obtaining ExitCode using Start-Process and WaitForExit instead of -Wait - Stack Overflow

I tried Adrian's solution and it works. But I used Wait-Process instead of relying on a side effect of retrieving the process handle. By posting your answer, you agree to the privacy policy and terms of service. By subscribing, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7. Join them; it only takes a minute: Obtaining ExitCode using Start-Process and WaitForExit instead of -Wait Ask Question. Here's a simplified test script: WaitForExit Write-Host "Process exit code should be here: ExitCode Running this script will cause notepad to be started. No ExitCode is provided when this is quit: Starting Notepad with -Wait - return code will be available Process finished with return code: Two things you could do I think Process object manually and bypass Start-Process Run the executable in a background job only for non-interactive processes!

Here's how you could do either: Start Out-Null Do Other Stuff Here Cheers, that's done the trick. By d efault, this cmdlet does not generate any output. Here is an example: ExitCode this will print 1 If you run it without -PassThru or -Wait, it will print out nothing.

Daniel McQuiston 1 8 Adrian 1 2. I would love to know why this works if anyone has any thoughts I'd appreciate them.

CarlR This is a quirk of the implementation of the. The implementation of the ExitCode property first checks if the process has exited. For some reason, the code that performs that check not only looks at the HasExited property but also verifies that the proces handle is present in the proces object and throws an exception if it is not.

powershell waitforexit example

PowerShell intercepts that exception and returns null. Accessing the Handle property causes the process object to retrieve the process handle and store it internally. Once the handle is stored in the process object, the ExitCode property works as expected. Or try adding this Greg Vogel 51 5. Handle after starting the process with Start-Process. It won't be set anymore after process exit.

fibonacci tutorial forex, forex binary options software free download

The '-Wait' option seemed to block for me even though my process had finished. Sign up or log in StackExchange. Sign up using Facebook. Sign up using Email and Password.

Powershell waitforexit example

Post as a guest Name. In it, you'll get: The week's top questions and answers Important community announcements Questions that need answers. Stack Overflow works best with JavaScript enabled. Worth noting the workaround: MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3. Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system