Powershell Script to Call Agent Job


#Save the following names as call_backup_job.ps1 in C:\temp

#Set instance and agent JobName
$Server = “MC\MSSQL2014”
$JobName = “myDBBackup FULL”

#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 N’$JobName'”

#Exec Command
$sqlCommand.ExecuteReader()

#Close Conneection
$sqlConn.Close()

#Call this ps1 file from Powershell
#
#powershell -noexit ‘C:\temp\call_backup_job.ps1’

Advertisements


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