Tuesday, November 9, 2010

PowerShell Script to Monitor the Status of a SQL Job

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

$server = new-object "Microsoft.SqlServer.Management.Smo.Server" "localhost"
$job = $server.JobServer.Jobs["Test Job"]
$now = Get-Date
do
{
 Start-Sleep -Seconds 1
 $job | select Name,CurrentRunStatus,LastRunDate
 $job.Refresh()
}
while($job.LastRunDate -lt $now)
$job | select Name,CurrentRunStatus,LastRunDate

3 comments:

inder said...

I am new to powershell.IF I pass vaiable instead of job name "Test Job" it does not work.what will be work around

inder said...

I am new to powershell.IF I pass vaiable instead of job name "Test Job" it does not work.what will be work around

Stepherd said...

Nice posts. Even i admired from your title of hiring Star Employee. Actually screening for the best and effective candidates is much tedious and also important thing for every HR team. Anyway thank you for this posts.

Manpower Consultancy in Chennai

Post a Comment