PowerShell: Get SQL Job History

$computername='sql01'

function getSqlJobHistory($sqlServerName){
  try{
    if(!(get-module SqlServer)){
      Install-Module -Name SqlServer
    }
    Import-Module -Name SqlServer
    $sqlServerInstance=Get-SqlInstance -ServerInstance $sqlServerName
    $sqlAgent=Get-SqlAgent -ServerInstance $sqlServerInstance.Name
    $sqlJobs=$sqlAgent|Get-SqlAgentJob|Get-SqlAgentJobSchedule|?{$_.IsEnabled}
    $sqlJobs|select-object Name,ActiveStartTimeOfDay,FrequencyTypes,FrequencySubDayTypes|ft -autosize
  }catch{
    write-warning $_
    return $false
  }
}

getSqlJobHistory $computername
PS C:\Users\sqladmin1> getSqlJobHistory sql01

Name                               ActiveStartTimeOfDay FrequencyTypes FrequencySubDayTypes
----                               -------------------- -------------- --------------------
Check Database Integrity.Subplan_1 03:00:00                     Weekly                 Once
Differential Database Backups      22:00:00                      Daily                 Hour
Daily 10PM                         22:00:00                      Daily                 Once
Beginning of Month                 00:00:00                    Monthly                 Once
Schedule                           00:30:00                      Daily                 Once
syspolicy_purge_history_schedule   02:00:00                      Daily                 Once
Schedule1                          00:00:00                      Daily               Second

Leave a Reply

Your email address will not be published. Required fields are marked *