惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
V
V2EX
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
美团技术团队
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks

博客园 - EricWen

IntelliJ IDEA 2024 安装与激活全攻略 Excel高手秘籍:50招助你效率翻倍 Navicat与DBeaver:数据库管理工具界的“蝙蝠侠”与“钢铁侠” 十年沉淀,重启开发之路 外网不能访问部署在虚机的NodeJs网站(80端口) Common Issues Which Cause Roles to Recycle How to remove a batch of VMs and related Disks How to create a batch of VMs with PowerShell How to build windows azure PowerShell Source Code 如何快速从一个Storage Account拷贝到另一个账号 比较Windows Azure 网站(Web Sites), 云服务(Cloud Services)and 虚机(Virtual Machines) Windows Azure 云服务角色架构 PowerShell Script to Deploy Multiple VM on Azure in Parallel #azure #powershell Use Windows Azure AD to create SSO projects 怎样提高Windows Azure Cloud Service中的WebRole的文件访问权限 Windows Azure 名词定义(Glossary) 怎样将SQL Azure数据库备份到本地或者Storage 自动备份SQL数据库到云存储Storage 数据库SQL Server DAC 导入导出数据到SQL Azure问题
如何批量删除虚拟机及其关联的存储(Windows Azure)
EricWen · 2014-02-24 · via 博客园 - EricWen

可以通过运行附件中PowerShell脚本文件RemoveVMandDisk.ps1批量删除VM和Disk,详细代码如下:

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. 打开Windows Azure PowerShell, 并导入订阅信息(Subscription)。

关于如何安装和配置Azure PowerShell,请参考http://www.windowsazure.com/en-us/documentation/articles/install-configure-powershell/

需要注意一点,在执行Get-AzurePublishSettingsFile命令获取Subscription文件的时候,不要使用它自动跳转的Url(这是国外Windows Azure的链接),需要手动输入https://manage.windowsazure.cn/publishsettings/index?client=powershell ,然后登录并获取Subscription文件

  1. 运行命令 Set-ExecutionPolicy RemoteSigned 更执行策略。
  2. 执行RemoveVMandDisk.ps1脚本。

语法:RemoveVMandDisk.ps1 <ServiceName> #可以删除以这个ServiceName开头的所有虚机VM

例子:RemoveVMandDisk.ps1 ericwenService

如果您在删除虚机后,删除Disk发生错误“当前磁盘被占用”,可以修改等待后台运行时间:Start-Sleep -Seconds 120* $azureDiskNames.Count

  1. 您还可以使用命令(Get-AzureDisk| where{ $_.AttachedTo -eq $null } | Remove-AzureDisk -DeleteVHD -Verbose )删除所有未使用(附加到VM上)的磁盘

以上脚本和代码仅供参考,您可以根据您的业务逻辑做调整和修改。