-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-RunbookUsingAlerts.ps1
More file actions
59 lines (51 loc) · 2.5 KB
/
Copy pathInvoke-RunbookUsingAlerts.ps1
File metadata and controls
59 lines (51 loc) · 2.5 KB
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
workflow Invoke-RunbookUsingAlerts
{
param (
[object]$WebhookData
)
# If runbook was called from Webhook, WebhookData will not be null.
if ($WebhookData -ne $null) {
# Collect properties of WebhookData.
$WebhookName = $WebhookData.WebhookName
$WebhookBody = $WebhookData.RequestBody
$WebhookHeaders = $WebhookData.RequestHeader
# Outputs information on the webhook name that called This
Write-Output "This runbook was started from webhook $WebhookName."
# Obtain the WebhookBody containing the AlertContext
$WebhookBody = (ConvertFrom-Json -InputObject $WebhookBody)
Write-Output "`nWEBHOOK BODY"
Write-Output "============="
Write-Output $WebhookBody
# Obtain the AlertContext
$AlertContext = [object]$WebhookBody.context
# Some selected AlertContext information
Write-Output "`nALERT CONTEXT DATA"
Write-Output "==================="
Write-Output $AlertContext.name
Write-Output $AlertContext.subscriptionId
Write-Output $AlertContext.resourceGroupName
Write-Output $AlertContext.resourceName
Write-Output $AlertContext.resourceType
Write-Output $AlertContext.resourceId
Write-Output $AlertContext.timestamp
Write-Output "`n$($AlertContext)"
# Act on the AlertContext data, in our case restarting the VM.
# Authenticate to your Azure subscription using Organization ID to be able to restart that Virtual Machine.
$cred = Get-AutomationPSCredential -Name "ContosoAccount"
Add-AzureAccount -Credential $cred
Select-AzureSubscription -subscriptionName "Visual Studio Professional with MSDN"
#Check the status property of the VM
Write-Output "Status of VM before taking action"
$VM = Get-AzureVM
Get-AzureVM -Name $AlertContext.resourceName -ServiceName $VM.ServiceName
Write-Output "Restarting VM"
# Restart the VM by passing VM name and Service name which are same in this case
Restart-AzureVM -ServiceName $VM.ServiceName -Name $AlertContext.resourceName
Write-Output "Status of VM after alert is active and takes action"
Get-AzureVM -Name $AlertContext.resourceName -ServiceName $VM.ServiceName
}
else
{
Write-Error "This runbook is meant to only be started from a webhook."
}
}