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

最新文章

  1. Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
  2. APP如何实现推送功能
  3. caca393刷PTP教程
  4. R-Studio
  5. python 缩进问题
  6. uva 147
  7. 关于ajax提交的公共接口的一大用处
  8. c语言:最长对称子串(3种解决方案)
  9. Slow HTTP Denial of Service Attack 漏洞解决
  10. 【Java集合系列】---总体框架
  11. 实战 | Android中文图混排时文图的居中对齐 FontMetrics以及自定义ImageSpan实现
  12. powerdesigner 使用
  13. python学习笔记14-函数
  14. hibernate的hql语句不支持 count(case...when ....else..)?
  15. mysql获得60天前unix时间示例
  16. Composer 自动加载(autoload)机制
  17. 基于Json.NET自己实现MVC中的JsonValueProviderFactory
  18. CF1143D/1142A The Beatles
  19. POJ 1655 Balancing Act(求树的重心)
  20. redis基本命令,配置参数

热门文章

  1. 【转】ETL数据增量抽取——通过触发器方式实现
  2. cocosstdio之字体之文本和FNT字体
  3. android中ADT和SDK的关系
  4. C++ 笔记(一) —— 尽量以 const、enum、inline 替换 #define
  5. monkey学习笔记
  6. JSTL基础
  7. AC日记——单词替换 1.7 21
  8. Google Play笔记之上架
  9. Win7虚拟机无法打开内核设备:\\Global\\vmx86
  10. QuickFIX/N入门:(三)如何配置QuickFIX/N