Pages

Wednesday, February 26, 2014

Checking SQL Server Agent jobs using Windows PowerShell

Two tables in particular are of interest to us to check for job execution information like job name, execution status, run date, run time, etc. - the sysjobs and sysjobhistory tables. 
The script below displays a list of jobs on your SQL Server instance with there status.
USE msdb 
GO 
SELECT 
   
j.[name] AS [JobName]
   
run_status CASE h.run_status 
   WHEN THEN 'Failed'
   
WHEN THEN 'Succeeded'
   
WHEN THEN 'Retry'
   
WHEN THEN 'Canceled'
   
WHEN THEN 'In progress'
   
END,
   
h.run_date AS LastRunDate,  
   
h.run_time AS LastRunTime
FROM sysjobhistory h 
   INNER JOIN sysjobs j ON h.job_id j.job_id 
       WHERE j.enabled 1  
       AND h.instance_id IN 
       
(SELECT MAX(h.instance_id
           
FROM sysjobhistory h GROUP BY (h.job_id))GO 
Notice that the run_date and run_time columns of the sysjobhistory table are of type int and would be a bit challenging to convert the columns to their appropriate data types.  Server Management Objects (SMO) exposes these properties when using Windows PowerShell. The JobServer property of the Server object represents the SQL Server Agent associated with an instance of SQL Server. This includes the SQL Server jobs, operators and alerts.
When translating the T-SQL query above to Windows PowerShell, we would be interested in the Name, LastRunDate andLastRunOutcome properties of the Jobs object. What's really good to note is that the LastRunDate property is in adatetime format that no longer requires conversion to the appropriate data type, similar to what we get from thesysjobhistory table in the msdb database.  I keep trying to highlight this for every tip I've written that uses PowerShell with SMO.  The only aspect of the code that we have changed from the scripts in the previous tips is the last line, i.e. adding new properties for the new objects we are working with. This highlights the power and simplicity of Windows PowerShell from the script.
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$rvs = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST\SQL2000"
#Create an instance of the Jobs object collection from the JobServer property
#And pipes that to the filter Where-Object cmdlet to retrieve only those jobs that are enabled but failed
$srv.JobServer.Jobs | Where-Object {$_.IsEnabled -eq $TRUE} | Select Name,LastRunOutcome, LastRunDate
 
Notice that the LastRunDate property is in the correct data type. The LastRunOutcome property is returned as they are without the need for further translations as in the T-SQL script above. Let's call to the Excel object as we did in the previous tips to format the results. Again, the script above is more than enough for what we need.
#Create a new Excel object using COM 
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)

#Counter variable for rows

$intRow = 1
#Read thru the contents of the SQL_Servers.txt file
foreach ($instance in get-content "D:\SQL_Servers.txt")
{
     #Create column headers
     $Sheet.Cells.Item($intRow,1) = "INSTANCE NAME:"
     $Sheet.Cells.Item($intRow,2) = $instance
     $Sheet.Cells.Item($intRow,1).Font.Bold = $True
     $Sheet.Cells.Item($intRow,2).Font.Bold = $True
     $intRow++
      $Sheet.Cells.Item($intRow,1) = "JOB NAME"
      $Sheet.Cells.Item($intRow,2) = "LAST RUN OUTCOME"
      $Sheet.Cells.Item($intRow,3) = "LAST RUN DATE"
     #Format the column headers
     for ($col = 1; $col รข€“le 3; $col++)
     {
          $Sheet.Cells.Item($intRow,$col).Font.Bold = $True
          $Sheet.Cells.Item($intRow,$col).Interior.ColorIndex = 48
          $Sheet.Cells.Item($intRow,$col).Font.ColorIndex = 34
     }
     $intRow++
      #######################################################
     #This script gets SQL Server Agent job status information using PowerShell
     [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
     # Create an SMO connection to the instance
     $srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance
     $jobs=$srv.JobServer.Jobs
     #Formatting using Excel

ForEach ($job in $jobs)  {

       # Formatting for the failed jobs 
       if ($job.LastRunOutcome -eq 0)
       
{
           $fgColor 3
       }
       else
       
{
           $fgColor 0
       }
   
       $Sheet.Cells.Item($intRow1=  $job.Name
       $Sheet.Cells.Item($intRow2$job.LastRunOutcome.ToString()
       $Sheet.Cells.item($intRow2).Interior.ColorIndex $fgColor
       $Sheet.Cells.Item($intRow3=  $job.LastRunDate

   
           
       $intRow ++
   

}
   $intRow ++

}
$Sheet.UsedRange.EntireColumn.AutoFit()
cls

No comments:

Post a Comment