Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Start-Job hangs Powershell on Windows Server 2016

$
0
0

I'm not sure where one reports bugs and a phone call to support the folks on the other end suggested I send a letter. I figured this was better.

Occasionally when running Start-Job the operation hangs. You can replicate this by creating a loop that spins off a bunch of jobs with a ScriptBlock. I haven't tested it without a ScriptBlock. Using threads to spin the jobs off to keep the main thread running I see multiple jobs that failed to start out of a loop of 200.

I've worked with jobs in older versions of Windows Server and never seen this before. Thought someone might want to know.

Here's a code block you can use to replicate. Just adjust the code for where you want log files to be dumped. You'll know it failed by having a missing log file for a job.

$ChildScriptBlock = {
	$Parameters = $args[0]
	$log = '<your path to log files>\' + $Parameters["jobName"] + '.txt'"Child Script: Got " + $Parameters["P1"] + " and " + $Parameters["P2"] + " With fallback of " + $Parameters["Fallback"]>> $log

	
	
}

$ParentScriptBlock = {
	$ParentParameters = $args[1]
	$ChildScriptBlock = $ParentParameters["ChildScriptBlock"]
	$log = '<your path to log files>\' + $ParentParameters["jobName"] + '.txt'
	$ChildParameters = @{
		Fallback = "fallback";
	}
	foreach ($key in $ParentParameters.Keys)
	{
		if ($key -ne "ChildScriptBlock")
		{
			$ChildParameters[$key] = $ParentParameters[$key]
		}
	}
	$job = Start-Job -Name $ParentParameters["jobName"] -ScriptBlock $ChildScriptBlock -ArgumentList $ChildParameters
}

$ParentParameters = @{
	P1 = "Parameter 1";
	P2 = "Parameter 2";
	ChildScriptBlock = $ChildScriptBlock;
}

$runspacePool = [runspacefactory]::CreateRunspacePool()
$runspacePool.Open()
$taskList = @()
for ($jobnumber = 1; $jobnumber -lt 200; $jobnumber++)
{
	$ParentParameters["jobName"] = "job_$jobnumber"
	$newThread = [powershell]::Create().AddScript($ParentScriptBlock).AddParameter('ParentParameters', $ParentParameters)
	$newThread.RunspacePool = $runspacePool
	$handle = $newThread.BeginInvoke()
	$taskList += New-Object -TypeName psobject -Property @{ "Handle" = $handle; "Thread" = $newThread; }
	Start-Sleep 1
}

$taskList | Where-Object { $_.Handle.IsCompleted } | ForEach-Object {
	$_.Thread.EndInvoke($_.Handle)
	$_.Thread.Dispose()
	$_.Thread = $_.Handle = $Null
}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>