-
Notifications
You must be signed in to change notification settings - Fork 0
/
AZ_Auto_Stop-Start_VMs_byRG.ps1
85 lines (69 loc) · 2.95 KB
/
AZ_Auto_Stop-Start_VMs_byRG.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
Param (
# Azure Subscription Id
[Parameter(Mandatory=$False)][ValidateNotNullOrEmpty()]
[String]
$AzureSubscriptionId="<< INSERT CLIENT AZ Subscription ID >>",
# Identify all VMs by their name
# String Format expected "vm01, vm02, vm03"
[Parameter (Mandatory = $False)]
[String]
$AzureVMList="All",
# Choose an action to perform
[Parameter (Mandatory = $false)]
[ValidateSet ("Start", "Stop", "Restart")]
[String]
$Action,
# Ressource Group
[Parameter(Mandatory=$False)][ValidateNotNullOrEmpty()]
[String]
$RG = "prod-automation-rg"
)
Connect-AzAccount -Identity -SubscriptionId $AzureSubscriptionId
## Perform choosed action to all VMs in list
if($AzureVMList -ne 'All') {
Write-Output "Azures VM Before [$AzureVMs]"
$AzureVMs = $AzureVMList.Split(",")
[System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs
Write-Output "Azures VM After [$AzureVMs]"
# Check if VM exists
$ValidAzureVMsToHandle=@()
foreach($VMName in $AzureVMsToHandle)
{
Write-Output " ============ Checking if VMName named [$($VMName.Trim())] exists "
$CheckVM = Get-AzVM | Where-Object { $_.Name -eq $($VMName.Trim()) }
Write-Output " ============ Checked: [$($CheckVM.Name)] "
if(!(Get-AzVM | Where-Object {$_.Name -like $($VMName.Trim()) })) {
Write-Warning "============ AzureVM : [$($VMName.Trim())] - Does not exist! - Check your inputs "
} else {
$ValidAzureVMsToHandle += $($VMName.Trim())
Write-Output " ============ Added VM [$($VMName.Trim())] to valid Azure VMs to handle: [$ValidAzureVMsToHandle] "
}
}
Write-Output " ";Write-Output " "
Write-Output " ============ Validated VMs: [$ValidAzureVMsToHandle]"
} else {
if (($null -eq $RG) -or ($RG -eq "")) {
throw " Ressource Group not defined - Check your inputs "
} else {
# Retrieve all VMs in the specified RG
$ValidAzureVMsToHandle = (Get-AzVM -ResourceGroupName $RG).Name
}
}
Write-Output " ";Write-Output " "
# Perform action on each VM
ForEach ($VMName in $ValidAzureVMsToHandle) {
Write-Output " ";Write-Output " "
Write-Output " ============ Perform action on VM [$VMName]"
## Getting name of the resource group which contains VM
$RG = (Get-AzVM | Where-Object {$_.Name -like $VMName }).ResourceGroupName
Write-Output " ============ Ressource Group found is [$RG]"
## Perform action
Switch ($Action) {
"Start" { Write-Output "Performing $Action action on $VMName"; Start-AzVM -Name $VMName -ResourceGroupName $RG
}
"Stop" { Write-Output "Performing $Action action on $VMName" ; Stop-AzVM -Name $VMName -ResourceGroupName $RG -Force
}
"Restart" { Write-Output "Performing $Action action on $VMName" ; Restart-AzVM -Name $VMName -ResourceGroupName $RG
}
}
}