Download - PowerShell Runspaces
Multithreading in PowerShell with runspaces
1/18/2017
Drew FurgiueleSenior DBA, IGS Energy
1/18/2017 2
About me
[email protected]@pittfurghttp://www.port1433.comhttp://www.genesface.com
Contact InfoI’m a Senior SQL Server DBA at IGS Energy in Dublin, Ohio. I’ve been using SQL Server since SQL Server 2000 and I love it. I also enjoy DevOps, release management, and PowerShell.
AGENDA
1/18/2017 3
1. Multithreading in PowerShell: Why?2. How it works3. Differences between runspaces and Invoke-Job4. Examples!
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
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
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
“… 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
Creating Runspaces
• CODE!
1/18/2017 8
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
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
Practical Example
• Master Data Management
1/18/2017 11
Learning More
• Runspaces – Hey Scripting Guy! https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/26/beginning-use-of-powershell-runspaces-part-1/
• Runspaces simplified as much as possible: https://blog.netnerds.net/2016/12/runspaces-simplified/
• Joe Prox’s PoshRSJob: https://github.com/proxb/PoshRSJob
1/18/2017 12
1/18/2017 13
About me
[email protected]@pittfurghttp://www.port1433.comhttp://www.genesface.com
Contact InfoI’m a Senior SQL Server DBA at IGS Energy in Dublin, Ohio. I’ve been using SQL Server since SQL Server 2000 and I love it. I also enjoy DevOps, release management, and PowerShell.