-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartStopVMs.ps1
182 lines (148 loc) · 5.89 KB
/
StartStopVMs.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
param (
[Parameter(Mandatory=$true)]
[String] $Action,
[Parameter(Mandatory=$false)]
[String] $TagName,
[Parameter(Mandatory=$false)]
[String] $TagValue
)
## Authentication
Write-Output ""
Write-Output "------------------------ Authentication ------------------------"
Write-Output "Logging into Azure ..."
# To test outside of Azure Automation, replace this block with Login-AzAccount
# Login-AzAccount
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
## End of authentication
## Getting all virtual machines
Write-Output ""
Write-Output ""
Write-Output "---------------------------- Status ----------------------------"
Write-Output "Getting all virtual machines from all resource groups ..."
try
{
if ($TagName)
{
$instances = Get-AzResource -TagName $TagName -TagValue $TagValue -ResourceType "Microsoft.Compute/virtualMachines"
if ($instances)
{
$resourceGroupsContent = @()
foreach ($instance in $instances)
{
$instancePowerState = (((Get-AzVM -ResourceGroupName $($instance.ResourceGroupName) -Name $($instance.Name) -Status).Statuses.Code[1]) -replace "PowerState/", "")
$resourceGroupContent = New-Object -Type PSObject -Property @{
"Resource group name" = $($instance.ResourceGroupName)
"Instance name" = $($instance.Name)
"Instance type" = (($instance.ResourceType -split "/")[0].Substring(10))
"Instance state" = ([System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo.ToTitleCase($instancePowerState))
$TagName = $TagValue
}
$resourceGroupsContent += $resourceGroupContent
}
}
else
{
#Do nothing
}
}
else
{
$instances = Get-AzResource -ResourceType "Microsoft.Compute/virtualMachines"
if ($instances)
{
$resourceGroupsContent = @()
foreach ($instance in $instances)
{
$instancePowerState = (((Get-AzVM -ResourceGroupName $($instance.ResourceGroupName) -Name $($instance.Name) -Status).Statuses.Code[1]) -replace "PowerState/", "")
$resourceGroupContent = New-Object -Type PSObject -Property @{
"Resource group name" = $($instance.ResourceGroupName)
"Instance name" = $($instance.Name)
"Instance type" = (($instance.ResourceType -split "/")[0].Substring(10))
"Instance state" = ([System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo.ToTitleCase($instancePowerState))
}
$resourceGroupsContent += $resourceGroupContent
}
}
else
{
#Do nothing
}
}
$resourceGroupsContent | Format-Table -AutoSize
}
catch
{
Write-Error -Message $_.Exception
throw $_.Exception
}
## End of getting all virtual machines
$runningInstances = ($resourceGroupsContent | Where-Object {$_.("Instance state") -eq "Running" -or $_.("Instance state") -eq "Starting"})
$deallocatedInstances = ($resourceGroupsContent | Where-Object {$_.("Instance state") -eq "Deallocated" -or $_.("Instance state") -eq "Deallocating"})
## Updating virtual machines power state
if (($runningInstances) -and ($Action -eq "Stop"))
{
Write-Output "--------------------------- Updating ---------------------------"
Write-Output "Trying to stop virtual machines ..."
try
{
$updateStatuses = @()
foreach ($runningInstance in $runningInstances)
{
Write-Output "$($runningInstance.("Instance name")) is shutting down ..."
$startTime = Get-Date -Format G
$null = Stop-AzVM -ResourceGroupName $($runningInstance.("Resource group name")) -Name $($runningInstance.("Instance name")) -Force
$endTime = Get-Date -Format G
$updateStatus = New-Object -Type PSObject -Property @{
"Resource group name" = $($runningInstance.("Resource group name"))
"Instance name" = $($runningInstance.("Instance name"))
"Start time" = $startTime
"End time" = $endTime
}
$updateStatuses += $updateStatus
}
$updateStatuses | Format-Table -AutoSize
}
catch
{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
elseif (($deallocatedInstances) -and ($Action -eq "Start"))
{
Write-Output "--------------------------- Updating ---------------------------"
Write-Output "Trying to start virtual machines ..."
try
{
$updateStatuses = @()
foreach ($deallocatedInstance in $deallocatedInstances)
{
Write-Output "$($deallocatedInstance.("Instance name")) is starting ..."
$startTime = Get-Date -Format G
$null = Start-AzVM -ResourceGroupName $($deallocatedInstance.("Resource group name")) -Name $($deallocatedInstance.("Instance name"))
$endTime = Get-Date -Format G
$updateStatus = New-Object -Type PSObject -Property @{
"Resource group name" = $($deallocatedInstance.("Resource group name"))
"Instance name" = $($deallocatedInstance.("Instance name"))
"Start time" = $startTime
"End time" = $endTime
}
$updateStatuses += $updateStatus
}
$updateStatuses | Format-Table -AutoSize
}
catch
{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
#### End of updating virtual machines power state