-
Notifications
You must be signed in to change notification settings - Fork 15
/
PowerManagement-AzFunction.ps1
142 lines (131 loc) · 5.87 KB
/
PowerManagement-AzFunction.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<#
.SYNOPSIS
Get, Start or Stop Azure resources like Application Gateways or VM through an Azure function configured with an Http trigger.
.DESCRIPTION
REQUIRED : Azure function with an Http trigger
REQUIRED : The Azure function is configured with a sytem identity and has power management privilege on the resources you will manage the power status.
.PARAMETER action
Mandatory
Supported values : get, start or stop
.PARAMETER type
Mandatory
Supported values : virtualMachines or applicationGateways
.PARAMETER name
Mandatory
Name of the resource you want to get, start or stop
.PARAMETER resource_group
Mandatory
Resource group name of the resource you want to get, start or stop
.NOTES
AUTHOR: James Dumont le Douarec
HttpStatusCode Enum: https://docs.microsoft.com/en-us/dotnet/api/system.net.httpstatuscode?view=netframework-4.8
.LINK
https://github.com/JamesDLD/AzureRm-PowerShell
https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-powershell
.EXAMPLE
1. Get the status of the vm "iis1" located in the resource group "apps-jdld-sand1-rg1"
curl --header "Content-Type: application/json" --request POST --data '{"action":"get","type":"virtualMachines","name":"iis1","resource_group":"apps-jdld-sand1-rg1"}' https://pws-powermgnt-apps2.azurewebsites.net/api/powermgnt?code=<API Token>
2. Stop the Application Gateway "appgateway1" located in the resource group "infr-jdld-noprd-rg1"
curl --header "Content-Type: application/json" --request POST --data '{"action":"stop","type":"applicationGateways","name":"appgateway1","resource_group":"infr-jdld-noprd-rg1"}' https://pws-powermgnt-apps2.azurewebsites.net/api/powermgnt?code=<API Token>
#>
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
# Interact with query parameters or the body of the request.
$body = $Request.Body
$action = $body.action
$type = $body.type
$name = $body.name
$resource_group = $body.resource_group
# Ensure that the system identity is enable.
if ($env:MSI_SECRET -and (Get-Module -ListAvailable Az.Accounts)) {
Try{
Write-Output "Connecting to Azure using the Azure function MSI."
$body = Connect-AzAccount -Identity -ErrorAction Stop
$status = [HttpStatusCode]::OK
}
Catch {$body = $_.Exception.Message;$status = [HttpStatusCode]::Unauthorized}
if ($action -and $type -and $name -and $resource_group) {
switch($type){
virtualMachines {
switch($action){
get {
Try{
$body = Get-AzVm -ResourceGroupName $resource_group -Name $name -status -ErrorAction Stop
$status = [HttpStatusCode]::OK
}
Catch {$body = $_.Exception.Message;$status = [HttpStatusCode]::Unauthorized}
}
start {
Try{
$body = Start-AzVm -ResourceGroupName $resource_group -Name $name -NoWait -ErrorAction Stop
$status = [HttpStatusCode]::OK
}
Catch {$body = $_.Exception.Message;$status = [HttpStatusCode]::Unauthorized}
}
stop {
Try{
$body = Stop-AzVm -ResourceGroupName $resource_group -Name $name -NoWait -Force -ErrorAction Stop
$status = [HttpStatusCode]::OK
}
Catch {$body = $_.Exception.Message;$status = [HttpStatusCode]::Unauthorized}
}
default {
$status = [HttpStatusCode]::BadRequest
$body="Invalid action. Allowed values : get, start, stop ..."
}
}
}
applicationGateways {
switch($action){
get {
Try{
$body = Get-AzApplicationGateway -ResourceGroupName $resource_group -Name $name -ErrorAction Stop
$status = [HttpStatusCode]::OK
}
Catch {$body = $_.Exception.Message;$status = [HttpStatusCode]::Unauthorized}
}
start {
Try{
$AppGw = Get-AzApplicationGateway -ResourceGroupName $resource_group -Name $name -ErrorAction Stop
$body = Start-AzApplicationGateway -ApplicationGateway $AppGw -ErrorAction Stop
$status = [HttpStatusCode]::OK
}
Catch {$body = $_.Exception.Message;$status = [HttpStatusCode]::Unauthorized}
}
stop {
Try{
$AppGw = Get-AzApplicationGateway -ResourceGroupName $resource_group -Name $name -ErrorAction Stop
$body = Stop-AzApplicationGateway -ApplicationGateway $AppGw -ErrorAction Stop
$status = [HttpStatusCode]::OK
}
Catch {$body = $_.Exception.Message;$status = [HttpStatusCode]::Unauthorized}
}
default {
$status = [HttpStatusCode]::BadRequest
$body="Invalid action. Allowed values : get, start, stop ..."
}
}
}
default {
$status = [HttpStatusCode]::BadRequest
$body="Invalid type. Allowed values : virtualMachines, applicationGateways."
}
}
}
else {
$status = [HttpStatusCode]::BadRequest
$body = "Please pass the following parameters : action, type, name, resource_group."
}
}
else {
$status = [HttpStatusCode]::Unauthorized
$body = "Please make that you have enabled the System assigned identity on your Azure function."
}
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})