Powershell SMO Job Object

I am trying to kick off a job using SMO objects. And encountered the following error:

Cannot find an overload for “Job” and the argument count: “1”. This simply means that the parameters weren’t used correctly. There should be two parameters, and should look something like this.  See the last line, below

#Load SMO Objects
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.Smo”) | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SmoExtended”) | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.ConnectionInfo”) | Out-Null

$SourceFullServerName = "DanServer";
$JobName = "Dan | Fail This Job";

#Set SMO Server object
$SourceServer = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $SourceFullServerName;

$Job = $SourceServer.JobServer.Jobs[$JobName];
$Job.Start();

At first I was using this, but got another error when starting the job
$Job = New-Object Microsoft.SqlServer.Management.Smo.Agent.Job ($SourceServer.JobServer, $JobName);
Exception calling “Start” with “0” argument(s): “Start failed for Job ‘Dan | Fail This Job‘.”

If you use the following code, it will provide a more detailed error message (reference here):
$error[0]|format-list -force;

This provides the following additional information:
Microsoft.SqlServer.Management.Smo.InvalidSmoOperationException: You cannot execute this operation since the object has not been created.

For more troubleshooting, add this line.
“Date Created: ” + $Job.DateCreated;
In my case, the job did not even return anything, so I did something wrong.

It turns out that I had a lot more luck with the following:

$Job = $SourceServer.JobServer.Jobs[$JobName];

Alternatively, if you want to use the JobID instead of the JobName, you can use the GetJobByID method:

$Job = $SourceServer.JobServer.GetJobByID($JobID);
Advertisements
This entry was posted in Powershell and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s