SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On



using powershell for sending notifications of failed jobs scheduled on SQL Serve... Expand / Collapse
Author
Message
Posted Tuesday, October 07, 2008 7:51 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 27, 2009 1:38 PM
Points: 45, Visits: 93
I typed the following code that I found on the Internet on my local machine using Notepad and I save it giving it an extention of PS1. Then I copied the file on the remote sql server machine. I also mapped to that drive (where I placed the file) on my local machine and I typed Set-ExecutionPolicy in PowerShell. When I try to run the script in Powershell it tells me it is not a script. What I am doing wrong?


[void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo");
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum");
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");

function GetFailedAgentJobs {
param ([string]$servername="127.0.0.1")
$srv = new-object('Microsoft.SqlServer.Management.Smo.server') $servername
$jobs = $srv.jobserver.jobs | where-object {$_.isenabled}
foreach ($job in $jobs) {
[int]$outcome = 0
[string]$output = ""
## Did the job fail completely?
if ($job.LastRunOutcome -ne "Succeeded") {
$outcome++
$output = $output + " Job failed (" + $job.name + ")" + " Result: " + $job.LastRunOutcome
}
## Did any of the steps fail?
foreach ($step in $job.jobsteps) {
if ($step.LastRunOutcome -ne "Succeeded"){
$outcome++
$output = $output + " Step failed (" + $step.name + ")" + " Result: " + $step.LastRunOutcome + " -- "
}
}
if ($outcome -gt 0) {
$obj = New-Object Object
$obj | Add-Member Noteproperty name -value $job.name
$obj | Add-Member Noteproperty lastrundate -value $job.lastrundate
$obj | Add-Member Noteproperty lastrunoutcome -value $output
$obj
}
}
}
Post #581802
« Prev Topic | Next Topic »


Permissions Expand / Collapse