zoukankan      html  css  js  c++  java
  • How to remove a batch of VMs and related Disks

    Foreword

    Need to remove a batch of VMs, which named with same prefix or belong to same Cloud Service. After remove VMs, should automatically remove related disk (OS disk & Data Disk) and related VHD file .

    Keywords

    PowerShell Get-AzureVM Remove-AzureVM

    Summary

    Write and run Windows Azure PowerShell script to implement this feature.

    Detailed

    1. The PowerShell script is below.

    param($serviceName)

    echo "Starting remove all vms of service $serviceName"

    #$serviceName="erictest"

    echo "Get all DiskNames of all VMs of service $serviceName."

    $azureDiskNames= Get-AzureDisk| where{$_.AttachedTo -ne $null -and $_.AttachedTo.HostedServicename.StartsWith($serviceName)} | select DiskName

    $azureDiskNames

    if($azureDiskNames -eq $null -or $azureDiskNames.Count -le 0){

    echo "No VMs wanted to Remove."

    exit

    }

    echo "`r`nStarting remove all VMs of service $serviceName..."

    Get-AzureVM | where{$_.ServiceName.StartsWith($serviceName)} | Remove-AzureVM -Verbose

    #It spends time to remove VM on backend.

    echo "Waiting Removing VM on backend..."

    Start-Sleep -Seconds 120* $azureDiskNames.Count

    echo "`r`nStarting remove all related disks..."

    foreach($diskName in $azureDiskNames){

    Get-AzureDisk | where {$_.DiskName -eq $diskName.DiskName } | Remove-AzureDisk -DeleteVHD -Verbose

    }

    echo "`r`nStarting remove all services"

    Get-AzureService | where{$_.ServiceName.StartsWith($serviceName)} | Remove-AzureService -Force -Verbose

    1. If want to remove all Disks not attached to any VM, can use one CmdLet : Get-AzureDisk| where{ $_.AttachedTo -eq $null } | Remove-AzureDisk -DeleteVHD -Verbose
    2. If want to create a batch of VMs with one data disk attached. How to create a batch of VMs with PowerShell

    Conclusion

    Reference

  • 相关阅读:
    iOS设计模式:观察者
    Java面向接口编程小例子
    《The DeadLine》(《最后期限》) 读后感
    Codeforces Round #395 Div1的A题Timofey and a tree
    重写和强制转换再调用能编译但不能运行
    Java继承和静态-加载顺序
    C++之pair
    用Java面向对象思想实现一个微博的功能(未完)
    Java对象在内存图示
    Java中OOP对象和引用
  • 原文地址:https://www.cnblogs.com/ericwen/p/3714825.html
Copyright © 2011-2022 走看看