Powershell Jobs

Towards the end of last week I found myself putting the finishing touches on my demo for my upcoming talk at the North Texas SQL Server User Group meeting. My demo is not anything flashy, I just wanted a way to get it up and running with as few clicks as possible. I just *knew* that I could use Powershell for this somehow so I looked into a handful of ways to get the job done but nothing seemed to be as simple as it should.

Enter Buck Woody (blog | @buckwoody).

I emailed Buck, gave him a quick description of what I was trying to do and he didn’t waste any time with an answer:

“Ah, yes, what you need is a ‘job’.”

“Thanks Buck, but I already have a job, what I need is to get this demo script finished.”

“No, you need a Powershell job.”

“Gee, thanks for the vote of confidence Buck, but I don’t think anyone is going to hire me just to do Powershell.”

“No, you fool, what you need is this.”

The idea is very simple, I simply want to start multiple connections to my SQL instance. The Start-Job cmdlet allows for this to happen. It kicks off your command as a background process and then goes onto the next line. So now my code can be reduced to just a few lines:

Add-PSSnapin SqlServerProviderSnapin100
Add-PSSnapin SqlServerCmdletSnapin100

Set-Location SQLSERVER:\SQL\SQLROCKSTAR-ONE\BACON

start-job -filepath "C:\WaitsAndQueuesAndYou\NTSSUG_demo1.ps1"
start-job -filepath "C:\WaitsAndQueuesAndYou\NTSSUG_demo2.ps1"
start-job -filepath "C:\WaitsAndQueuesAndYou\NTSSUG_demo3.ps1"
start-job -filepath "C:\WaitsAndQueuesAndYou\NTSSUG_demo4.ps1"
start-job -filepath "C:\WaitsAndQueuesAndYou\NTSSUG_demo5.ps1"

And I get the chance to monitor and capture some activity, which is what I wanted.

Thanks Buck, for all of your help last week.

2 thoughts on “Powershell Jobs”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.