-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetFailedJobs.ps1
33 lines (28 loc) · 994 Bytes
/
GetFailedJobs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<#
# Runon : Azure
#>
# Get the Azure Automation connection object
$connection = Get-AutomationConnection -Name "<SPI>"
# Connect to Azure using the connection object
Try {
Connect-AzAccount -ServicePrincipal `
-Tenant $connection.TenantID `
-ApplicationId $connection.ApplicationID `
-CertificateThumbprint $connection.CertificateThumbprint | Out-Null
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
# Set the subscription context
Set-AzContext -SubscriptionId "<Sub_Id>" | Out-Null
$query = "AzureDiagnostics | where ResultType == 'Failed' | project TimeGenerated, RunbookName_s, RunOn_s ,ResultType | where TimeGenerated > now() - 4h | sort by TimeGenerated asc"
$WorkspaceID = "WorkspaceID"
$kqlQuery = Invoke-AzOperationalInsightsQuery -WorkspaceId $WorkspaceID -Query $query
If ($kqlQuery.Results) {
$kqlQuery.Results
# Send Email (If needed)
}
else {
Write-Output "There is no failure job since last 4 hours."
}