Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
November 10, 2022

More Runspaces and Parallel Processing

In the last article, we began exploring alternatives that would allow us to scale commands. This is especially useful for commands that might take long to run. There isn't any reason this command needs to run sequentially.

$r = Get-Content .\company.txt -PipelineVariable pv | ForEach-Object {
    Get-WinEvent -FilterHashtable @{Logname = "System"; Level = 2 } -ComputerName $_
    #simulate network latency, a large number of computers, and latency.
    Start-Sleep -Milliseconds (Get-Random -Minimum 500 -Maximum 3000  )
} | Select-Object @{Name = "Computername"; Expression = { $pv } },
TimeCreated, ID, ProviderName, Message

Each Get-WinEvent command is independent of the others. As such, if I can run them in parallel, I should see a performance gain. On my computer, querying seven computers and returning about 2500 event log entries took about 20 seconds.

One of the alternatives we looked at toward the end of the previous article was using a set of custom runspaces.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.