Windows Azure Powershell remove virtual machines and VHD

The scripts can remove virtual machines and cloud server and VHD, but can't remove storage account 

点击(此处)折叠或打开

  1. param($serviceName)
  2. echo "Starting remove all vms of service $serviceName"
  3. #$serviceName="gasfef"
  4. echo "Get all DiskNames of all VMs of service $serviceName."
  5. $azureDiskNames= Get-AzureDisk| where{$_.AttachedTo -ne $null -and $_.AttachedTo.HostedServicename.StartsWith($serviceName)} | select DiskName
  6. $azureDiskNames
  7. if($azureDiskNames -eq $null -or $azureDiskNames.Count -eq 0){
  8. echo "No VMs wanted to Remove."
  9. exit
  10. }
  11. echo "`r`nStarting remove all VMs of service $serviceName..."
  12. Get-AzureVM | where{$_.ServiceName.StartsWith($serviceName)} | Remove-AzureVM -Verbose
  13. #It spends time to remove VM on backend.
  14. echo "Waiting Removing VM on backend..."
  15. Start-Sleep -Seconds 120 $azureDiskNames.Count
  16. echo "`r`nStarting remove all related disks..."
  17. foreach($diskName in $azureDiskNames){
  18. Get-AzureDisk | where {$_.DiskName -eq $diskName.DiskName } | Remove-AzureDisk -DeleteVHD -Verbose
  19. }
  20. echo "`r`nStarting remove all services"
  21. Get-AzureService | where{$_.ServiceName.StartsWith($serviceName)} | Remove-AzureService -Force -Verbose

  22. echo "`r`Starting remove all StorageAccountName"
  23. Get-AzureStorageAccount |select StorageAccountName | Remove-AzureStorageAccount -Verbose


10-04 03:39