While searching for ways to trigger SQL Server Agent Job via Powershell (using .NET methods) I have seen many suggestions that "really" do not utilize .NET methods, but are powershell that trigger either TSQL procedures or SQLPS, or are required to run locally..
What i was missing was a generic method that works with remote SQL servers. (WS-Management protocol/WMI/RPC or whatever..)
-- Here is a simple variant that only utilize SMO!
# load SMO
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
# define
[String]$sqlServer = "your_server"
[String]$sqlJob = "your_jobname"
$sqlSrv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $sqlServer
$matchSqlAgentJob = $sqlSrv.JobServer.Jobs | where-object { $_.Name -eq $sqlJob }
# exec for all enabled
foreach ($activeSqlAgentJob in ($matchSqlAgentJob | Where-Object {$_.IsEnabled -eq "$TRUE" }))
{
Write-Output "Found matching SQL Server Agent Jobs:"
Write-Output $activeSqlAgentJob.Name
# actual trigger
$sqlSrv.JobServer.GetJobById($activeSqlAgentJob.JobID).start()
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment