Quantcast
Channel: Questions in topic: "sql-agent"
Viewing all articles
Browse latest Browse all 80

Can powershell script in sql job step call the next step in the same job

$
0
0
I need to call Step 2 only if criteria is met and it is a certain time of day. Here is the PS script for Step 1 Step 1 - check for files $Checktime = Get-Date $jobName = "Check For Files" $stepName = "Send No Invoices Email" IF ( (Test-Path -Path "\\test\data\AP\*.csv") -eq $true ) { $sqlConnection = new-object System.Data.SqlClient.SqlConnection $sqlConnection.ConnectionString = 'server=test;integrated security=TRUE;database=msdb' $sqlConnection.Open() $sqlCommand = new-object System.Data.SqlClient.SqlCommand $sqlCommand.CommandTimeout = 120 $sqlCommand.Connection = $sqlConnection $sqlCommand.CommandText= "exec dbo.sp_start_job APProcess" $sqlCommand.ExecuteNonQuery() $sqlConnection.Close() } ElseIf ( ($Checktime -ge (Get-Date 8:00) -and $Checktime -lt (Get-Date 9:00) ) ) {$sqlCMD = "EXEC dbo.sp_start_job N'{0}', @step_name = N'{1}'" -f $jobName, $stepName $sqlCMD } Step 2 - Send Specfic email I tried to raise an error and on failure go to step 2, but the first step actually failed and sent incorrect emails. I also tried not assigning the jobname and it failed with syntax error. Can PA initiate a step in the same job?

Viewing all articles
Browse latest Browse all 80

Trending Articles