Years ago I wrote a script that utilized a runspace. It worked great when it worked. Some environments the script wouldn't work properly and I'd have to run the original version that took longer. The difference was of little consequence though so I never troubleshot the issue. I've moved back in-house and now my environment is one of the ones where the runspace isn't functioning like I need. Time to fix it since I want to utilize runspaces for other scripts here too.
What I've found is that $GPO shows up inside $ScriptBlock missing data in most of the properties. There is a second argument sent to $ScriptBlock that shows up just fine. Since the script has worked in some locations and not others I'm having trouble figuring out what exactly is wrong. In the past it was run from the same machine in different environments so my assumption is the local machine running it isn't an issue.
Attempting to cut things down to something easier to read. I put lines with "######################" around the areas where $GPO works and doesn't work for reference.
What I've found is that $GPO shows up inside $ScriptBlock missing data in most of the properties. There is a second argument sent to $ScriptBlock that shows up just fine. Since the script has worked in some locations and not others I'm having trouble figuring out what exactly is wrong. In the past it was run from the same machine in different environments so my assumption is the local machine running it isn't an issue.
Attempting to cut things down to something easier to read. I put lines with "######################" around the areas where $GPO works and doesn't work for reference.
Code:
######################
########### ScriptBlock
######################
$ScriptBlock = {
Param (
$GPO,
$LinkedGPOIDs
)
######################
# The line below here shows $GPO with 2 of 12 properties with the proper data.
# Two other properties show information, but it's not the expected data.
# That leaves up to 8 properties that are nulled out upon arrival in the scriptblock
# (Some values may be null to begin with)
$GPO > C:\Users\Me\Desktop\GPO.txt
######################
# LinkedGPOIDs shows up without issue
# Code snipped where I play with $GPO
# I gather everything up and output what I need. The output is wrong due to the garbage coming into the scriptblock though.
Return $OutputData
}
######################
########### Start
######################
$Throttle = 10
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle)
$RunspacePool.Open()
$Jobs = @()
# Queue the jobs
ForEach ($GPO in $GPOList) {
######################
# $GPO below shows me an object with 12 parameters that typically have at least 10/12 properties with data
$GPO
######################
$Job = [Powershell]::Create().AddScript($ScriptBlock).AddArgument($GPO).AddArgument($LinkedGPOIDs)
$Job.RunspacePool = $RunspacePool
$Jobs += New-Object PSObject -Property @{
Pipe = $Job
Result = $Job.BeginInvoke()
}
}
# Wait for jobs to finish
Write-Host "The GPOs are being processed, please wait.." -NoNewline
Do {
Write-Host "." -NoNewline
Start-Sleep -Seconds 1
} While ( $Jobs.Result.IsCompleted -contains $false)
Write-Host "`nProcessing has completed. Your data is being collected and report generated ..."
# Gather the results
$Results = @()
ForEach ($Job in $Jobs) {
$Results += $Job.Pipe.EndInvoke($Job.Result)
$Job.Pipe.Dispose()
}