Technical Article

Start a job at a defined step on another sql instance

,

This is a PowerShell script that can start a job at a defined step name (default will be step 1), on a network instance either on a network on locally.

###########################################################################
#
# AUTHOR:  Aziz Zerzouri
#
# COMMENT: Start job on remote server.
#
# VERSION HISTORY:
# 1.0 05/17/2014 - Initial release
#
###########################################################################
#region init
Set-StrictMode -Version "latest";
$DebugPreference = "Stop";
$erroractionpreference = "Stop";
#endregion
##################################################################



#region check for errors
function CheckForErrors 
{
throw "job failed";
exit 1;
} 
#endregion


#region start  Job

Function StartSqlJob()
{
param ([string]$server, [string]$JobName,[string]$StepName)
try     
{
#Create/Open Connection
$sqlConn = new-object System.Data.SqlClient.sqlConnection "server=$Server;database=msdb;Integrated Security=sspi"
$sqlConn.Open()

#Create Command Obj
$sqlCommand = $sqlConn.CreateCommand()
$sqlCommand.CommandText = "EXEC dbo.sp_start_job @job_name = N'$JobName', @step_name = N'$StepName'"
#Exec Command
$sqlCommand.ExecuteReader()
Start-Sleep -s 10
#get status of  job
Get-SQLJobStatus -Server $Server -JobName $JobName;
####################
#Close Connection
$sqlConn.Close();

}
catch     
{         
CheckForErrors     
}
}
#endregion
#region Get-SQLJobStatus

Function Get-SQLJobStatus
{
    param ([string]$Server, [string]$JobName)
try
{
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Connect ionInfo")
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum ")
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
# Initilize SMO Objects
$svr = New-Object Microsoft.SqlServer.Management.Smo.Server $Server
   if($svr -eq $null){CheckForErrors;}
$agent = $svr.JobServer;
$retVal= $agent.Jobs | Where-Object {$_.Name -eq $JobName}
[string]$sts = $retVal.CurrentRunStatus;
$continue = $true;
$svr = $null;
$agent = $null;
# Check for Job Existence
If ($retVal-eq $null)
{
      Throw "$JobName does not exist on $Server";
      $continue = $false;
  
}

#Continue if job does not have any status yet

Start-Sleep -s 60;

while($continue)
{  
    
$svr = New-Object Microsoft.SqlServer.Management.Smo.Server $Server
   if($svr -eq $null)
{
CheckForErrors;
}
$agent = $svr.JobServer;
 $jobCurrStatus = $agent.Jobs | Where-Object {$_.Name -eq $JobName} | Select CurrentRunStatus;
[string]$sts = $jobCurrStatus.CurrentRunStatus;
 if($sts -eq "Idle")
{
$svr = $null;
$agent = $null;
StartSqlJob $Server $JobName $StepName;
}
Else
{
    while($sts -eq "Executing")
   {
      
        $sts = $null;
$svr = $null;
            $agent = $null;

$svr = New-Object Microsoft.SqlServer.Management.Smo.Server $Server
   if($svr -eq $null)
{
CheckForErrors;
}
$agent = $svr.JobServer;
$jobCurrStatus = $agent.Jobs | Where-Object {$_.Name -eq $JobName} | Select CurrentRunStatus;
    $sts = $jobCurrStatus.CurrentRunStatus;
Start-Sleep -s 300;
   }
$jobLastRunOutcome = $agent.Jobs | Where-Object {$_.Name -eq $JobName} | Select LastRunOutcome;
$joboutcome = $jobLastRunOutcome.LastRunOutcome
#Write-Output $joboutcome
                                                 if ($joboutcome -ne "Succeeded")
     {
   $svr = $null;
   $agent = $null;
   CheckForErrors
 }
     Else
     {
$svr = $null;
$agent = $null;
Break;
 }
}

}       


}
catch     
{         
CheckForErrors     
}
}
#endregion


#region main function
function Main
{ 
Param([string]$Server, [string]$JobName, [string]$StepName) 

try
{
### Start the SQL Job at Step X here and wait.
 StartSqlJob $Server $JobName $StepName;
}

catch     
{         
CheckForErrors     
}
}
#endregion


#region hard coded values
$Server = "ServerName";
$JobName = "JobName";
$StepName = "StepName";

###########
#
Main $Server $JobName $StepName;
#
###########
#endregion

Rate

3 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

3 (1)

You rated this post out of 5. Change rating