Search code examples
powershellremotinginvoke-commandstart-process

Powershell start-process -wait parameter fails in remote script block


So here's a sample of what I'm trying to do:

Invoke-Command [Connection Info] -ScriptBlock {
    param (
        [various parameters]
    )
    Start-Process [some .exe] -Wait
} -ArgumentList [various parameters]

It connects to the other machine just fine, and launches the process fine. The problem is it doesn't wait for the process to complete before moving on. This causes issues. Any ideas?

Quick edit: why does the -Wait parameter fail when running the process remotely?


Solution

  • This is the problem with Powershell version 3, but not version 2 where -Wait works as it should.

    In Powershell 3 .WaitForExit() does the trick for me:

    $p = Start-Process [some .exe] -Wait -Passthru
    $p.WaitForExit()
    if ($p.ExitCode -ne 0) {
        throw "failed"
    }
    

    Just Start-Sleep until .HasExited - doesn't set .ExitCode, and it's usually good to know how your .exe finished.