[Azure]使用Powershell批量开启ASM虚拟机(带状态检测和重试)

60 篇文章 0 订阅
59 篇文章 0 订阅

本脚本用于批量启动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 checked
        if ($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 again
            Start-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 loop
    if ($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


  • 50
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值