From 793e856db9353c76442751ef6ea134fabafd49bd Mon Sep 17 00:00:00 2001 From: PaulHCode <71329201+PaulHCode@users.noreply.github.com> Date: Fri, 16 Oct 2020 13:07:51 -0400 Subject: [PATCH] Update Remove-AzrVirtualMachine.ps1 When I tested it with my VM with managed disks it didn't remove the OS disk or data disks. It does with these changes. --- Azure/Remove-AzrVirtualMachine.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Azure/Remove-AzrVirtualMachine.ps1 b/Azure/Remove-AzrVirtualMachine.ps1 index 668a74a..a59c7fd 100644 --- a/Azure/Remove-AzrVirtualMachine.ps1 +++ b/Azure/Remove-AzrVirtualMachine.ps1 @@ -113,6 +113,7 @@ function Remove-AzrVirtualMachine { } else { ## managed Get-AzDisk | where { $_.ManagedBy -eq $vm.Id } | Remove-AzDisk -Force + Get-AzDisk | Where-Object { $_.id -eq $vm.StorageProfile.OsDisk.ManagedDisk.Id } | Remove-AzDisk -Force } ## Remove any other attached disks @@ -123,6 +124,11 @@ function Remove-AzrVirtualMachine { $dataDiskStorageAcct | Remove-AzStorageBlob -Container $uri.Split('/')[-2] -Blob $uri.Split('/')[-1] } } + Else { + foreach ($disk in $vm.StorageProfile.DataDisks) { + Remove-AzDisk -DiskName $Disk.name -ResourceGroupName $vm.ResourceGroupName -Force + } + } } if ($Wait.IsPresent) {