Powershell Runspace

Status
Not open for further replies.

Sog

Ars Tribunus Militum
1,575
I'm attempting to learn how to use a runspace in Powershell. I've been able to integrate most of my code, but the line beginning with XML below has given me trouble in the snippet below:

Code:
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle)
$RunspacePool.Open()
$Jobs = @()
 
ForEach ($GPO in $GPOList) {
	# Create an XML report of the GPO that can be used to query for information.
	$Report = $Null
	[XML]$Report = Get-GPOReport -GUID $GPO.ID -ReportType XML
	$Job = [Powershell]::Create().AddScript($ScriptBlock).AddArgument($GPO).AddArgument($LinkedGPOIDs).AddArgument($Report)
	$Job.RunspacePool = $RunspacePool
	$Jobs += New-Object PSObject -Property @{
		Pipe = $Job
		Result = $Job.BeginInvoke()
   }
}

When I set $Report within the ForEach and add the argument to the runspace I'm able to get my desired results. I'd rather the report be generated within the scriptblock. When the line is run within the scriptblock $Report has a value for the first thread, but not for subsequent threads. Other variables set within the scriptblock end up in the results without issue (ran a test generating a new variable that pulled $GPO.DisplayName and a random number to monitor whether data was being changed or not).

Would anyone be able to shed some light on what I could do to have the xml report generated within the scriptblock? I saw some warnings that threads could share variables, but did not locate further information to explain how that works or how to avoid the scenario.
 

Cookie.Monster

Ars Scholae Palatinae
666
Runspaces can be a pain, but they can be well worth it in time saved! In general, treat them as completely segregated. This means the Group Policy module would need to load inside every runspace. There is a way to set the initial session state to load this; if I recall correctly when I was running this, there wasn't really a performance benefit to doing so, but worth exploring.

I've had trouble with AddArgument in the past, it almost seemed like it stopped passing arguments if I tacked too many on. Eventually, I started limiting this by passing a custom object with a property for each variable I wanted to pass. Doesn't sound like the issue here though.

One thing you can do to troubleshoot is within the runspace, write-verbose or write to a log file with the output from get-variable or other commands at different stages in your scriptblock. It might help identify where things are going wrong.

I could be wrong, but I was under the impression that you needed to explicitly set up a variable to be shared between runspaces, it's not something that just happens.

Good luck! I need to brush up on my runspace functions. Made some generic ones that I use to speed up a bunch of tasks and ad hoc stuff at work, but never found a good way to set up a timeout for each runspace (i.e. if one of the runspaces hangs, I need to ctrl-c or end PowerShell when everything but that runspace is complete, killing any cleanup or other post-run tasks I had scripted out).
 

Sog

Ars Tribunus Militum
1,575
Thanks for the reply, Cookie. I did a proof of concept with Start-Job earlier in the week. Something had me run into your PS cheat sheet post and I hit up your blog and looked around. The runspace posts were new to me and made me take a stab at them.

My scriptblock contained the GP module. I caught on to that one quick enough. I did some troubleshooting with writing variables to my output and caught that $Report works for the first runspace, but none of the subsequent runspaces. I hadn't thought of writing to a separate log to monitor things that way. I'll see what I can do. It's weird that the first one works and anything after that comes up null. If I can figure out what I'm doing wrong I think it'll cut the time down on the script substantially. It's a few seconds each time a report is created.
 
Status
Not open for further replies.