-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-AzureRmVMState.ps1
More file actions
83 lines (66 loc) · 2.72 KB
/
Set-AzureRmVMState.ps1
File metadata and controls
83 lines (66 loc) · 2.72 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<#
Sets Azure Rm VM to a desired state
Version: 0.2
Author: Dirk Dulfer
Date: 31 July 2018
Change state of size of an Azure RM Virtual Machine based on parameters passed into this script
Param ([object]$VM, $DesiredState)
# parameters
[object]$VM - Expected content and structure:
@{ "Name" = $vm.Name; "ResourceGroupName" = $vm.ResourceGroupName }
[string]$DesiredState -
'on', 'off' or a valid VM size (supported by this particular VM)
# TODO:
- [ ] clean shutdown prior to updating size
[ ] add support for controling VMs in other subscriptions
#>
[CmdletBinding()]
[OutputType([string])]
Param ([object]$VM, $DesiredState)
$ConnectionName = "AzureRunAsConnection"
Write-Verbose 'Get the connection "AzureRunAsConnection"'
$servicePrincipalConnection = Get-AutomationConnection -Name $ConnectionName
Write-Output "Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | Out-Null
Write-Output "`tProcessing $($VM.Name)..."
if (("on", "off") -contains $DesiredState.ToLower()) {
$v = Get-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Status
if (($v.Statuses.DisplayStatus -contains "VM running") -and ($DesiredState.ToLower() -eq "off")) {
Write-Output "`tSwitching $DesiredState now..."
$v | Stop-AzureRmVM -Force
}
elseif ($v.Statuses.DisplayStatus -contains "VM deallocated" -and $DesiredState.ToLower() -eq "on") {
Write-Output "`tSwitching $DesiredState now..."
$v | Start-AzureRmVM
}
elseif ($v.Statuses.DisplayStatus -contains "VM running" -and $DesiredState.ToLower() -eq "on") {
Write-Output "`tVM already on..."
}
elseif ($v.Statuses.DisplayStatus -contains "VM deallocated" -and $DesiredState.ToLower() -eq "off") {
Write-Output "`tVM already deallocated..."
}
else {
Write-Output "Unknown state, skipping..."
}
}
else {
$v = Get-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name
if ($v.HardwareProfile.VMSize.ToString().ToLower() -ne $DesiredState.ToLower()) {
Write-Output "`tScaling machine from $($v.HardwareProfile.VMSize.ToLower()) to $DesiredState"
# resize the VM
$v.HardwareProfile.VmSize = $DesiredState
$v | Update-AzureRmVm
$v = $v | Get-AzureRmVM -ResourceGroupName -Status
if ($v.Statuses.DisplayStatus -contains "VM deallocated") {
Write-Output "`tSwitching on now..."
$v | Start-AzureRmVM
}
}
else {
Write-Output "`tMachine in desired state ($DesiredState), skipping..."
}
}