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 
  • Why would you not want to use normal PowerShell job for this? – Jim B Jun 03 '16 at 12:35
  • Open to suggestions if there is an easier way to do what i'm trying to accomplish. All of my scripts thus far have all been very linear. – Bill Lindsay Jun 03 '16 at 12:37
  • Take a look at https://blogs.technet.microsoft.com/heyscriptingguy/2012/12/31/using-windows-powershell-jobs/ seems to me that background jobs are what you are looking for – Jim B Jun 03 '16 at 12:41
  • Gotcha, yeah I had actually tried something along those lines but for some reason it actually takes longer to run than the linear code. Ill edit the original post with that code – Bill Lindsay Jun 03 '16 at 12:47

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
  • 206
  • 1
  • 8