Home Dashboard Directory Help

PowerShell ignores "-wait" request on Start-Process when PS is run as a win32 job by LanDen Labs



Sign in
to vote
Type: Bug
ID: 776078
Opened: 1/8/2013 2:21:17 PM
Access Restriction: Public
User(s) can reproduce this bug


If you run PowerShell from a cmd.exe window created by Powershell, the 2nd instance no longer waits for jobs to complete. The 2nd instance is a child of a win32 job as reported by Process Explorer.

I discovered this when my C# Explorer program is used to open a cmd.exe window and PS is run from that window, it also ignores the -Wait request. It appears that any PowerShell which is run from a cmd.exe which is a 'win32 job' fails to honor the wait request.

I ran into this with PowerShell version 3.0 on windows 7/x64
Sign in to post a comment.
Posted by Dave E 968 on 7/15/2013 at 3:38 AM
This may be related to http://connect.microsoft.com/PowerShell/feedback/details/774903/powershell-3-0-start-process-disregards-wait-parameter-when-invoked-remotely ?
Posted by Dave E 968 on 7/15/2013 at 3:28 AM
I am experiencing a similar issue.

If I run:
Start-Process -Wait notepad.exe

a) From a standard PowerShell (3.0) instance, then it waits as expected.
b) From a PowerShell (3.0) instance where I have started it with the 'run as different user' feature, then it no longer waits.

I am running this on Windows Server 2008 R2
Sign in to post a workaround.
Posted by LanDen Labs on 7/22/2013 at 9:29 AM
Correction - the work around does work. The script has a flaw and needs the full path to the batch file it is making.
Posted by LanDen Labs on 7/22/2013 at 8:56 AM
The workaround does not work for me.

Running the following script by launching with the -Version 2 switch still fails to wait.

# Test Powershell's ability to wait for background job.
# The ability to wait is required to run some of the PowerShell scripts.

# Seconds for background scripts to wait. Must be longer than it takes PowerShell to start the scripts.
$seconds = 15
$testBat = 'test-wait.bat'

echo "dir" | out-file -force -encoding ascii $testBat
# echo ("timeout " + $seconds) | out-file -append -force -encoding ascii $testBat
echo "ping -n 15 >nul" | out-file -append -force -encoding ascii $testBat
# echo "exit 1" | out-file -append -force -encoding ascii $testBat

$test1_batch = { start-process cmd.exe -ArgumentList "/c test-wait.bat" -wait -WindowStyle Minimized }
$test2_batch = { start-process cmd.exe -wait -ArgumentList "/c test-wait.bat" }
$test3_batch = { & test-wait.bat}

echo "----- Test running batch file in minimized window -----"
$ElapsedTime = [System.Diagnostics.Stopwatch]::StartNew()
Start-Job $test1_batch -Name "Test1-1"
Start-Job $test1_batch -Name "Test1-2"
Start-Job $test1_batch -Name "Test1-3"
#wait untill all the background task complete
get-job | wait-job

$elapsedStr = $ElapsedTime.Elapsed.ToString()
if ($ElapsedTime.Elapsed.Seconds -lt $seconds)
echo ("wait test#1 FAILED, should take more than "+ $seconds + " seconds, but only took: " + $ElapsedTime.Elapsed.Seconds.ToString() + " seconds")
echo ("wait test#1 PASSED, took: " + $ElapsedTime.Elapsed.Seconds.ToString() + " seconds")
Posted by JeeCay on 3/20/2013 at 7:23 AM
Call script with powershell.exe -Version 2 -File "./Script.ps1". This will let it work on systems where you have got PS 3.0 installed. I did not use special PS 3.0 cmdlets, so I cannot tell what happens when you running this command line while using PS 3.0 cmdlets.