3

Trying to multithread some of my scripts that take a while to run. One example is getting the last login for a user. It checks all of our DC's and then returns the most recent time. We have quite a few and they are global so running sequentially takes a while.

I saw this answer How do I run my PowerShell scripts in parallel without using Jobs?

which got me going in setting up the runspace and running it but I am not sure how to get the data back.

This is what I have so far

$username = Read-Host "Enter the Users ID"
$dcs = Get-ADDomainController -Filter {Name -like "*"} | Select -expandproperty name
$Code = {
  Param($username,$dc)
  Get-ADUser $username | Get-ADObject -Server $dc -Properties lastLogon | 

Select -Expandproperty lastLogon
  }
$rsPool = [runspacefactory]::CreateRunspacePool(1,8)
$rsPool.Open()
foreach($dc in $dcs)
  {
  $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($username).AddArgument($dc)
  $PSinstance.RunspacePool = $RunspacePool
  $PSinstance.BeginInvoke()
  }

So I just need to wait for each job to finish and then capture the results of each which is what I am not sure how to do

Edit: Also I had previously tried to do this with jobs but the code actually took longer than the normal scrip

$userName = Read-Host "Enter NTID: "

$time = 0


$dcs = Get-ADDomainController -Filter {Name -like "*"} | Select -expandproperty name


$scriptbox = {

Param($username,$dc)

Get-ADUser $username | Get-ADObject -Server $dc -Properties lastLogon | Select -Expandproperty lastLogon

}   

foreach($dc in $dcs){start-Job -ScriptBlock $scriptbox -ArgumentList $username,$dc}


Get-Job | Wait-Job


Get-Job

$Data = ForEach ($Job in (Get-Job)) {

Receive-Job $Job

Remove-Job $Job

}


Foreach ($date in $Data){if($date -gt $time){$time = $date}}


$dt = [DateTime]::FromFileTime($time)

write-Host $username "last logged on at:" $dt 

1 Answers1

2

You're right to use Runspaces instead of the *-Job cmdlets as they are much faster!

I recently posted AsyncTcpScan which leverages Runspaces and is incredibly fast! It can be easily modified to run any scriptblock. Below is what your original script should look like after being integrated.

WARNING: I wasn't able to test the following code. I made some changes to your original script based on my experience working with the Active Directory cmdlets.

# Script to run in each thread.
[System.Management.Automation.ScriptBlock]$ScriptBlock = {

    $adUser = Get-ADUser -Identity $args[0] -Server $args[1] -Properties lastLogon

    $result = New-Object PSObject -Property @{ 'User'      = $args[0];
                                               'DC'        = $args[1];
                                               'LastLogon' = $adUser.LastLogon; }

    return $result

} # End Scriptblock

function Invoke-AsyncJob
{
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true)]
        [System.String]
        # User ID
        $Username
    )

    Import-Module -Name ActiveDirectory

    $Results = @()

    $AllJobs = New-Object System.Collections.ArrayList

    $AllDomainControllers = Get-ADDomainController -Filter "*"

    $HostRunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(2,10,$Host)

    $HostRunspacePool.Open()

    foreach($DomainController in $AllDomainControllers)
    {
        $asyncJob = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($($Username,$($DomainController.Name)))

        $asyncJob.RunspacePool = $HostRunspacePool

        $asyncJobObj = @{ JobHandle   = $asyncJob;
                          AsyncHandle = $asyncJob.BeginInvoke()    }

        $AllJobs.Add($asyncJobObj) | Out-Null
    }

    $ProcessingJobs = $true

    Do {

        $CompletedJobs = $AllJobs | Where-Object { $_.AsyncHandle.IsCompleted }

        if($null -ne $CompletedJobs)
        {
            foreach($job in $CompletedJobs)
            {
                $result = $job.JobHandle.EndInvoke($job.AsyncHandle)

                if($null -ne $result)
                {
                    $Results += $result
                }

                $job.JobHandle.Dispose()

                $AllJobs.Remove($job)
            } 

        } else {

            if($AllJobs.Count -eq 0)
            {
                $ProcessingJobs = $false

            } else {

                Start-Sleep -Milliseconds 1000
            }
        }

    } While ($ProcessingJobs)

    $HostRunspacePool.Close()
    $HostRunspacePool.Dispose()

    return $Results

} # End function Invoke-AsyncJob
phbits
  • 236