Last active
July 27, 2020 17:59
-
-
Save SQLDBAWithABeard/fcde818486dd4aad843ea3325ddec00e to your computer and use it in GitHub Desktop.
notify teams agent results all jobs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$SQLInstances = | |
$startdate = (Get-Date).AddHours(-12) | |
$TeamsWebHook = "" | |
# Import-Module 'C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.107\dbatools.psd1' | |
$AllJobs = " | |
SqlInstance...|...Total...|...Successful...|...FailedJobs...|...FailedSteps...|...Canceled... | |
--------------------------------------------- | |
" | |
foreach ($Instance in $SQLInstances) { | |
Write-Host "Connecting to $instance" | |
try{ | |
$smo = Connect-DbaInstance $Instance -ErrorAction Stop | |
Write-Host "Connected successfully to $instance" | |
} | |
catch{ | |
Write-Host "Failed to connect to $Instance" | |
$errorMessage = $_ | Out-String | |
Write-Host $errorMessage | |
Continue | |
} | |
Write-Host "Getting Agent Jobs on $instance" | |
try { | |
$AgentJobs = Get-DbaAgentJobHistory -SqlInstance $smo -EnableException -StartDate $startdate | |
Write-Host "Successfully got Agent Jobs on $instance" | |
} | |
catch { | |
Write-Host "Failed to get agent jobs on $Instance" | |
$errorMessage = $_ | Out-String | |
Write-Host $errorMessage | |
Continue | |
} | |
$jobs = $agentJobs | |
$NumberOfJobs = ($Jobs |Where-Object {$PSitem.StepId -eq 0}).Count.ToString("00") | |
$NumberOfFailedJobs = ($Jobs |Where-Object {$PSitem.StepId -eq 0}| Where-Object {$PSItem.Status -eq 'Failed'}).StepName.Count.ToString("00") | |
$NumberOfFailedJobSteps = ($Jobs |Where-Object {$PSitem.StepId -ne 0}| Where-Object {$PSItem.Status -eq 'Failed'}).StepName.Count.ToString("00") | |
$NumberOfSuccessfulJobs = ($Jobs |Where-Object {$PSitem.StepId -eq 0} | Where-Object {$PSItem.Status -eq 'Succeeded'}).StepName.Count.ToString("00") | |
$NumberOfCanceledJobs = ($Jobs |Where-Object {$PSitem.StepId -eq 0} | Where-Object {$PSItem.Status -eq 'Canceled'}).StepName.Count.ToString("00") | |
Write-Host "SqlInstance $Instance - Number of Jobs $NumberOfJobs - Number of Successful Jobs $NumberOfSuccessfulJobs - Number of Failed Jobs $NumberOfFailedJobs" | |
$AllJobs = $AllJobs + "$($Instance.Split('.')[0])..........<b>$NumberOfJobs</b>................<b>$NumberOfSuccessfulJobs</b>.........................<b>$NumberOfFailedJobs</b>............................<b>$NumberOfFailedJobSteps</b>..............................<b>$NumberOfCanceledJobs</b>........ | |
" | |
try{ | |
$smo.ConnectionContext.Disconnect() | |
Write-Host "Disconnecting $instance" | |
} | |
catch{ | |
Write-Host "Failed disconnect from $Instance" | |
$errorMessage = $_ | Out-String | |
Write-Host $errorMessage | |
Continue | |
} | |
} | |
Write-Host "Since $startdate" | |
Write-Host "$AllJobs" | |
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
$webhookurl = $TeamsWebhook | |
$allJobsMessage = $AllJobs | |
$Text = @" | |
# Overview of SQL Agent Jobs in Production since $startdate | |
$allJobsMessage | |
"@ | |
$JSONBody = [PSCustomObject][Ordered]@{ | |
"@type" = "MessageCard" | |
"@context" = "http://schema.org/extensions" | |
"summary" = "Overview for the last 12 hours" | |
"themeColor" = '0078D7' | |
"sections" = @( | |
@{ | |
"activityTitle" = "Job Failures " | |
"activitySubtitle" = "Overview for the last 12 hours since $startdate" | |
"activityImage" = "https://thumbs.dreamstime.com/b/summary-concept-people-letters-icons-colored-flat-vector-illustration-isolated-white-background-summary-concept-142699906.jpg" | |
"text" = $allJobsMessage | |
"markdown" = $true | |
} | |
) | |
} | |
$TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100 | |
$parameters = @{ | |
"URI" = $webhookurl | |
"Method" = 'POST' | |
"Body" = $TeamMessageBody | |
"ContentType" = 'application/json' | |
} | |
Invoke-RestMethod @parameters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment