Home Dashboard Directory Help
Search

powershell.exe, cmd.exe, or start.exe invoked ps1 scripts fail if they contain start-job in a for loop by John_T


Status: 

Active


3
0
Sign in
to vote
Type: Bug
ID: 564994
Opened: 6/3/2010 11:09:50 AM
Access Restriction: Public
0
Workaround(s)
view
3
User(s) can reproduce this bug

Description

When invoking a powershell script from powershell.exe, cmd.exe, or start.exe e.g:
cmd.exe /c powershell -nologo -noninteractive "C:\Scripts\Powershell\test.ps1"

, and the powershell script contains a for loop invoking the start-job commandlet e.g:

$ServerList = @(
"SERVER-1",
"SERVER-2"
)

for ($i=0; $i -lt $ServerList.Length; $i++)
{
Start-Job -ScriptBlock {$x=0}
}
, the powershell engine stop unexpectedly:

Engine state is changed from Available to Stopped.

Details:
NewEngineState=Stopped
PreviousEngineState=Available

SequenceNumber=10

HostName=ConsoleHost
HostVersion=2.0
HostId=1cb1812d-7fb8-460a-a2ad-cae0d31d1a4a
EngineVersion=2.0
RunspaceId=39f941a4-6d6f-495d-acd8-c89dbd70aa8b
PipelineId=
CommandName=
CommandType=
ScriptName=
CommandPath=

Fault bucket 719312874, type 5
Event Name: PowerShell
Response: Not available
Cab Id: 0

Problem signature:
P1: powershell.exe
P2: 6.1.7600.16385
P3: System.ObjectDisposedException
P4: System.ObjectDisposedException
P5: tomation.Remoting.OutOfProcessTextWriter.WriteLine
P6: System.IO.FileStream.Write
P7: unknown
P8:
P9:
P10:

When run in interactive mode, this behavior does not happen.
Details
Sign in to post a comment.
Posted by lyvwhy on 4/20/2012 at 3:33 PM
I just killed my self for the last couple of hours on this error. It seemed to be a problem with running jobs when attempting to exit form powershell. I added this at the end of my script to fix the problem.

This basically waits for all the jobs to finish before exiting.

$jobs = (get-job -state running | Measure-Object).count
Write-host "Currently waiting for exis" -Foreground Yellow
while($job)    
        {
        Write-Host "Currently running jobs"
        get-job -state running
        start-sleep -seconds 5
        $jobs = (get-job -state running | Measure-Object).count
        }
Posted by Nobytes on 12/7/2010 at 6:56 PM
I have something similar but it does not involve a for-loop.

If I use 'runas' to run a powershell script, and the script contains a 'Start-Job' scriptblock, powershell crashes with:

Problem signature:
Problem Event Name:    PowerShell
NameOfExe:    powershell.exe
FileVersionOfSystemManagementAutomation:    6.1.7600.16385
InnermostExceptionType:    System.ObjectDisposedException
OutermostExceptionType:    System.ObjectDisposedException
DeepestPowerShellFrame:    tomation.Remoting.OutOfProcessTextWriter.WriteLine
DeepestFrame:    System.IO.FileStream.Write
ThreadName:    unknown
OS Version:    6.1.7600.2.0.0.272.7
Locale ID:    3081
Sign in to post a workaround.