powershell runspaces

13
Multithreading in PowerShell with runspaces 1/18/2017 Drew Furgiuele Senior DBA, IGS Energy

Upload: drew-furgiuele

Post on 14-Apr-2017

202 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: PowerShell Runspaces

Multithreading in PowerShell with runspaces

1/18/2017

Drew FurgiueleSenior DBA, IGS Energy

Page 2: PowerShell Runspaces

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.

Page 3: PowerShell Runspaces

AGENDA

1/18/2017 3

1. Multithreading in PowerShell: Why?2. How it works3. Differences between runspaces and Invoke-Job4. Examples!

Page 4: PowerShell Runspaces

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

Page 5: PowerShell Runspaces

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

Page 6: PowerShell Runspaces

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

Page 7: PowerShell Runspaces

“… 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

Page 8: PowerShell Runspaces

Creating Runspaces

• CODE!

1/18/2017 8

Page 9: PowerShell Runspaces

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

Page 10: PowerShell Runspaces

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

Page 11: PowerShell Runspaces

Practical Example

• Master Data Management

1/18/2017 11

Page 12: PowerShell Runspaces

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

Page 13: PowerShell Runspaces

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.