4. Multithreading with PowerShell
• Most PowerShell scripts execute serially
• Which is usually fine!
• … Except when it isn’t
• Some things don’t always run fast, despite all the
optimization in the world
• And if something runs “fast” when applied to n+1
targets, it might become slow
1/18/2017 4
5. Runspaces – The good
• Runspaces spin up new threads on an existing
process for parallel execution.
• Threads can be invoked asynchronously
• Can automatically manage the number of
threads executing (so you don’t overwhelm
your CPU)
• Can return pretty much anything you want
1/18/2017 5
6. Runspaces – The not so good
1/18/2017 6
• Can be complex to set up and configure
• Need monitored
• Need cleaned up
• Are hard to troubleshoot
• Modules don’t persist
7. “… so preoccupied with whether or not you could…”
• “…that you didn't stop to think if you should.”
• Multithreading is a great tool to keep in your
toolbox, but it’s not a hammer looking for a nail
• Ever heard of failing at scale?
• How about failing at scalex?
1/18/2017 7
9. What about Start-Job / Receive-Job?
• Start-Job incurs a lot of overhead
• No throttling
• Same goes for –AsJob and Invoke-Command
• Does have a –ThrottleLimit, but that just limits
concurrent commands, NOT sessions
• How much of a difference? Let’s see it in action.
1/18/2017 9
10. Practical Example #1
• Reading Error Logs
• Text parsing in PowerShell is an expensive, slow
process
• Breaking up the workload into threads can vastly
improve performance
1/18/2017 10