本脚本用于批量启动ASM某个云服务下的全部或部分虚拟机,调用脚本前需要导入定于或使用Add-AzureAccount -Environment AzureChinaCloud登陆。
脚本如下:
param(#The name of the subscription to take all the operations within. [Parameter(Mandatory = $true)] [string]$SubscriptionName, # The name of the cloudservice.[Parameter(Mandatory = $true)][string]$ServiceName,# "VM Name 1;VM Name 2;VM Name 3;VM Name 4;...;VM Name n", if not assigned, start all vms under the cloudservice[Parameter(Mandatory = $false)][string]$VMNames = $NULL
)$vms = $NULL;
if($VMNames -eq "")
{$vms = (Get-AzureVM -ServiceName $ServiceName).Name;
} else {$vms = $VMNames.TrimEnd(";").Split(";");
}
$CheckedVM = New-Object System.Collections.ArrayList;Select-AzureSubscription -SubscriptionName $SubscriptionName;while($true)
{foreach($vmName in $vms){# check if the VM is already checkedif ($CheckedVM.Contains($vmName)){continue;} # get the VM object$vm = Get-AzureVM -ServiceName $ServiceName -Name $vmName;if ($vm.Status -eq "StoppedDeallocated"){# if the VM is still stopped(Deallocated), try start it againStart-AzureVM -ServiceName $ServiceName -Name $vmName;} else {# if the VM is not in stopped(Deallocated) status, meaning that it's starting or started, so skip it$CheckedVM.Add($vmName);}}# if all VMs have been checked, quit while loopif ($CheckedVM.Count -eq $vms.Count){break;}
}
调用方法:
启动云服务下全部虚拟机:
[ASM]safe_start_VMs.ps1 -SubscriptionName <Subscription Name> -ServiceName <CloudService Name>
OperationDescription OperationId OperationStatus
-------------------- ----------- ---------------
Start-AzureVM 6c0a6d72-1ecd-4310-890d-fe8cea588b4d Succeeded
Start-AzureVM 9296f1ca-7af3-4114-a4a3-223d5bef9f11 Succeeded
Start-AzureVM 6a814b66-8341-458e-9dce-bef177336aac Succeeded
Start-AzureVM 3665031a-37b0-4a1c-bee4-938cbe783bb7 Succeeded
0
1
2
3
启动云服务下部分虚拟机:
[ASM]safe_start_VMs.ps1 -SubscriptionName <Subscription Name> -ServiceName <CloudService Name> -VMNames "<VM1 Name>;<VM2 Name>"
OperationDescription OperationId OperationStatus
-------------------- ----------- ---------------
Start-AzureVM 70086a7c-fd9e-4694-af3d-0ceeb30307d6 Succeeded
Start-AzureVM 4261277b-6fb3-4e0b-ae7d-1b078af8a43a Succeeded
0
1