Start a SQL Agent Job with the .NET Framework from PowerShell

As of this writing, the most recent version of the SQLServer PowerShell module (which installs as part of SQL Server Management Studio) includes cmdlets for retrieving information about SQL agent jobs, but no cmdlets for starting them.

startsqljob1a

I recently ran into a situation where I needed to start a SQL agent job from PowerShell. The solution needed to be a tool that others could use who may or may not have the SQLServer module, SQLPS module or older SQL Server snap-in installed.

I decided to write a function to leverage the .NET Framework from PowerShell to start a SQL Server job:

The job returns a Boolean. True means it started successfully and false means it failed to start:

startsqljob2a

The Start-MrSqlAgentJob function shown in the previous code example can be downloaded from my SQL repository on GitHub.

Update

Thanks to Rob Sewell for pointing out that Get-SqlAgentJob returns a SMO object which has a start method:

startsqljob4a

That means it can be used to start a SQL agent job:

startsqljob3a

It does require that SQL Server Management Studio 2016 be installed on the machine it’s being run from.

µ

1 Comment

  1. SQLDBAwithTheBeard

    Good work

    (Get-SQLAgentJob -Instance Server -Name Jobnane).start() will work and stop too as GetSqlagentjob returns a smo object

    Reply

Leave a Reply

%d bloggers like this: