如何批量删除虚拟机及其关联的存储(Windows Azure)

可以通过运行附件中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上)的磁盘

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

转载于:https://www.cnblogs.com/ericwen/p/3564566.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值