PowerShell Workflow bulk start virtual machines in Azure


Here is a runbook for start specific vms in Azure.

 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
workflow Bulk-Start
{
Param
(
[Parameter(Mandatory=$true)]
[string[]]$VMs
)

#Connect to Azure Account via Azure user account
$Cred = Get-AutomationPSCredential -Name {Azure Account}
Add-AzureAccount -Credential $Cred
#Select specific subscription within your account
Select-AzureSubscription -SubscriptionName {subscription}

foreach ($V in $VMs)
{
$VM = Get-AzureVM –ServiceName 'SDDSC-DEMO' -Name $V
if ($VM.PowerState -eq "Started")
{
# The VM is already started, so send notice
Write-Output ($VM.InstanceName + " is already running")
}
else
{
# The VM needs to be started
$StartRtn = Start-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName -ErrorAction Continue

if ($StartRtn.OperationStatus -ne 'Succeeded')
{
# The VM failed to start, so send notice
Write-Output ($VM.InstanceName + " failed to start")
}
else
{
# The VM started, so send notice
Write-Output ($VM.InstanceName + " has been started")
}
}
}
}

Publish this runbook into Azure, and you can run the runbook like below:

1
Bulk-Start -VMs dsc-demo001, dsc-svr001