-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slack-FlipVMs.ps1
73 lines (68 loc) · 2.22 KB
/
Slack-FlipVMs.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
workflow Shutdown-Start-VMs-By-Resource-Group
{
Param
(
[Parameter(Mandatory=$true)]
[ValidateSet("csv","xml","clixml")]
[String]
$AzureResourceGroup,
[Parameter(Mandatory=$true)]
[string]
$Shutdown,
[Parameter(Mandatory=$true)]
[String]
$Tag
)
write-output $AzureResourceGroup
$shutdown = [system.convert]::ToBoolean($shutdown)
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
$null = Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
#ARM VMs
Get-AzureRmVM -ResourceGroupName $AzureResourceGroup | ForEach-Object {
if($_.Tags.Values -contains $Tag) {
if($Shutdown -eq $true){
"Stopping '$($_.Name)' ...";
$null = Stop-AzureRmVM -ResourceGroupName $AzureResourceGroup -Name $_.Name -Force;
}
else{
"Starting '$($_.Name)' ...";
$null = Start-AzureRmVM -ResourceGroupName $AzureResourceGroup -Name $_.Name;
}
}
};
#ASM VMs
Get-AzureRmResource | where { $_.ResourceGroupName -match $AzureResourceGroup -and $_.ResourceType -eq "Microsoft.ClassicCompute/VirtualMachines"} | ForEach-Object {
if($Shutdown -eq $true){
if($_.PowerState -eq "Started"){
"Stopping '$($_.Name)' ...";
$null = Stop-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force;
}
}
else{
if($_.PowerState -eq "Stopped"){
"Starting '$($_.Name)' ...";
$null = Start-AzureVM -ServiceName $_.ServiceName -Name $_.Name;
}
}
}
return
}