Skip to content

Instantly share code, notes, and snippets.

@nanoDBA
Created March 23, 2023 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanoDBA/c2bdb00da0d313cce2dd1f6a22929e71 to your computer and use it in GitHub Desktop.
Save nanoDBA/c2bdb00da0d313cce2dd1f6a22929e71 to your computer and use it in GitHub Desktop.
Console Output RED/GREEN failures for SQL Agent job history, multiple key property sorts. Attempting to imitate the output of the SQL Server Management Studio Job History window in a console.
# Console Output RED/GREEN failures for SQL Agent job history, multiple key property sorts
# attempting to imitate the output of the SQL Server Management Studio Job History window
$paramHash = @{
SqlInstance = 'YOURSERVER01','YOURSERVER02' # comma separated list of SQL Server instances
StartDate = "$(((Get-Date).AddDays(-.1) ))" # 0.1 days ago - ARE YOU SURE? This does not mean 1 day. It means 2.4 hours ago.
# StartDate = "$(((Get-Date).AddDays(-90) ))" # 90 days ago
EndDate = "$((Get-Date ))" # now
Job = 'Some SQL Agent Job Name goes here' # name of SQL Agent job goes here
ExcludeJobSteps = $false
}
Get-DbaAgentJobHistory @paramHash |
Select-Object @{Name = "_"; #dumb name for a property where checkmark or × will go
Expression = { $status = $_.status
switch ($status) { "Failed" { "$([char]0x1b)[91m×"
Break
}
"Succeeded" { "$([char]0x1b)[92m$([char]8730)"
Break
}
}
}
},
#RunDate, StepID, SqlInstance, JobName, StepName, Duration, Message, SqlSeverity, SqlMessageID, OperatorEmailed, OperatorNetsent, OperatorPaged, RetriesAttempted | `
RunDate, StepID, SqlInstance, JobName, StepName, Duration, SqlSeverity, SqlMessageID, OperatorEmailed, OperatorNetsent, OperatorPaged, RetriesAttempted, Message | `
<# multiple key sort follows #>
Sort-Object -Property @{e = { $_.SqlInstance } }, @{e = { $_.RunDate }; Ascending = $false }, @{e = { $_.StepID }; Ascending = $false } | `
#Sort-Object -Property RunDate -Descending | ` # single key sort
Format-Table -Wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment