Long time DBA, but new to PowerShell. I'm looking to use PowerShell to loop through several SQL Server instances and find out what jobs failed within the past 24 hours. I need to know failures even if the job ran successfully afterwards. Right now I just want to get it to work on one server, and then I'll move on to multiple servers.
What I have so far allows me to loop through all the jobs, but I'm not sure what to do to get the execution statuses for the last 24 hours:
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" "localhost"
$jobs = $srv.JobServer.Jobs
foreach ($job in $jobs)
{
$jobHistory = $job.EnumHistory()
}
Any help would be appreciated. I'm looking forward to getting in to PowerShell more, but right now, some of the SMO is a bit confusing.
Thanks, Dan