-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunbook-Alert-AzBackupFailures.ps1
88 lines (83 loc) · 3.1 KB
/
Runbook-Alert-AzBackupFailures.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
$VerbosePreference = "Continue"
$WebhookURL = ""
# Ensures you do not inherit an AzureRMContext in your runbook
Disable-AzContextAutosave -Scope Process
$connection = Get-AutomationConnection -Name AzureRunAsConnection
while(!($connectionResult) -And ($logonAttempt -le 10))
{
$LogonAttempt++
# Logging in to Azure...
$connectionResult = Connect-AzAccount `
-ServicePrincipal `
-Tenant $connection.TenantID `
-ApplicationID $connection.ApplicationID `
-CertificateThumbprint $connection.CertificateThumbprint
Start-Sleep -Seconds 10
}
$VMsFailedToBackup = @()
$RecoveryVaults = Get-AzRecoveryServicesVault
write-output "There are $($RecoveryVaults.count) Recovery Service Vaults"
for ($i=0; $i -lt $RecoveryVaults.count; $i++)
{
$containers = Get-AzRecoveryServicesBackupContainer -BackupManagementType AzureVM -VaultId $RecoveryVaults.ID -ContainerType AzureVM
write-output "Found $($containers.count) containers that need to be backed up"
for ($a=0; $a -lt $containers.count; $a++)
{
$BackupItem = Get-AzRecoveryServicesBackupItem -Container $Containers[$a] -WorkloadType AzureVM -VaultId $RecoveryVaults[$i].ID | Where-Object {$_.LastBackupStatus -eq "Failed"}
if ($BackupItem)
{
$VMsFailedToBackup += $BackupItem.VirtualMachineId.split("/")[-1].ToUpper()
}
}
}
if ($VMsFailedToBackup.count -eq 0)
{
write-output "No VMs failed to backup"
}
else
{
$AttachmentColor = "#FF0000"
$Title = "Alert Fired :fire:"
$SlackMsgPreJson = @{
text= "*VMs Failed to Backup*"
attachments= @(
@{
color= "$AttachmentColor"
title= "$Title"
author_name = "Azure Alert"
fields= @(
@{
title = "Affected";
value = "$VMsFailedToBackup";
short = $true;
},
@{
title = "Recovery Vault";
value = "$($RecoveryVaults.name)";
short = $true;
},
@{
title = "Error";
value = "Check Azure logs for more details under the Recovery Vault: $($RecoveryVaults.name)";
short = $false;
}
)
}
)
}
try
{
if ($WebhookURL)
{
$SlackMsgJson = $SlackMsgPreJson | ConvertTo-Json -Depth 100
$resultInvokeWebRequest = Invoke-RestMethod -Method Post -Uri $WebhookURL -Body $SlackMsgJson -ContentType 'application/json'
}
return $resultInvokeWebRequest
}
catch
{
$ErrorMessage = $_.Exception.Message
write-error "Error inside $($myinvocation.mycommand): $ErrorMessage"
return "Error inside $($myinvocation.mycommand): $ErrorMessage"
}
}