SQL Server Agent Jobs Status from Multiple instances through powershell query

The below powershell script brings information about SQL Server Agent jobs and its status from more than one SQL Server instance.


CLEAR
$HOSTNAME='COMPUTERNAMEHERE'
Import-Module sqlps -DisableNameChecking
$servernames=@("computername\namedinstancename1",
"computername\namedinstancename2",
"computername\namedinstancename3",
"computername\namedinstancename4",
"computername\namedinstancename5"
)
$servernames|
ForEach-Object{
    $computername=$_
    #Get a server object which corresponds to the default instance
    $srv=New-Object `
    -TypeName 'Microsoft.SQLServer.Management.Smo.Server' `
    -ArgumentList $computername
    $srv.JobServer.Jobs|
    Select-Object OriginatingServer,Name,LastRundate,LastRunOutcome,NextRunDate,CurrentRunstatus|
    Where-Object LastRunOutcome -EQ "Failed"|
    Where-Object {$_.Name -like "*JobNameHere*"}|
    Sort-Object LastRundate -Descending|
    Format-Table -AutoSize

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server