Powershell: having issues with trying to use RunspacePool and passing an Array as an argument

Ignatius Booreguard 1 Reputation point
2021-09-30T22:18:58.477+00:00

Been fighting learning how to use PS Runspaces for a large AD user processing task that has to run daily so I can multi-thread the whole thing. The goal is to query AD for all users first so as to not overwhelm the domain controllers with individual queries. The list is about 14K of users and growing, and that takes about a day to process currently as is single threaded.

Powershell:

$Worker = {
    Param($EID,$syncallADUsers)
    Write-Host "Processing $EID"
    #testing if the array is even passed <- I just did this to test if the array was passed at all
    Write-Host "First ADUser in list is $($syncallADUsers[0].DistinguishedName)"

    $Result = $syncallADUsers | Where-Object {$_.EmployeeID -eq $EID}

    Write-Host "Here are the results for $EID : $($Result.DistinguishedName)"
}

$MaxRunspaces = 5

$SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $MaxRunspaces, $SessionState, $Host)
$RunspacePool.Open()

$Jobs = New-Object System.Collections.ArrayList

$syncallADUsers = [System.Collections.ArrayList]::Synchronized($allADUsers)

$termList | ForEach-Object{
        Write-Host "Creating runspace for $($_.EmployeeID)"
        $PowerShell = [powershell]::Create()
        $PowerShell.RunspacePool = $RunspacePool
        $PowerShell.AddScript($Worker).AddArgument($_.EmployeeID).AddArgument($syncallADUsers) | Out-Null

         $JobObj = New-Object -TypeName PSObject -Property @{
        Runspace = $PowerShell.BeginInvoke()
        PowerShell = $PowerShell  
    }

    $Jobs.Add($JobObj) | Out-Null
}

Variables that are not shown:
$termlist = A set of EmployeeIDs that were imported with an Import-CSV statement
$allADUsers = A set of ADUsers all queried from Active Directory (Get-ADUser -filter * -Properties EmployeeID)

I get all the output that I expect until I attempt the "Where-Object" statement. I can run this manually with a test EID in the console and it works as expected. In this job it just returns nothing. I see the processor go nuts; so, it's actually trying to search, but never returns anything.

I see the first "Processing $EID" line with the expected EID there.

Then I see "First ADUser in list is $($syncallADUsers[0].DistinguishedName) and array is $($syncallADUsers.count) big" line exactly as it should with the Distinguished Name of the first item of the SyncAllADUser returned (so the array is there) and then I see the count of it as consistent.

Then paradoxically I see the last line "Here are the results for $EID : $($Result.DistinguishedName)" as : "Here are the results for 329334 : " ... No result but I test that in the console and I get what I would expect.

I'm flummoxed! Any help would be greatly appreciated.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
4,325 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,891 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 37,771 Reputation points
    2021-10-04T10:02:56.013+00:00

    Hello @Ignatius Booreguard ,

    Thank you for your question and for getting in touch.

    We have a topic referring to a problem similar to yours, I believe it will be useful for you, see it on the link below:

    https://social.technet.microsoft.com/Forums/en-US/86bcf0f9-b364-4090-9739-9e8d756baac4/help-using-runspaces?forum=winserverpowershell

    ------------------------------------------------------------------------------------------------------------------------

    If the answer is helpful, please vote positively and accept as an answer.

    0 comments No comments