@Billsborough, Scott/PDX Thank you for your patience over the matter!!!
I am done with my repro and below are my findings:
I tried to replicate the same environment in terms of the current condition as you mentioned in the query where you have 2 VM in separate Resource Groups with same name.
First I tried the mentioned script which you were referring to and I can confirm I am getting the same error as per the Error handling outlined in the code:
Then I tried running your script as it is and it was executed successfully without any error:
As per the error mentioned by you in the query it seems that there is a possibility that the associated disk with Azure VM was not found under the Subscription. Now there can be a lot of reasons for same but one among them what I can think of is maybe the disk was deleted from portal itself beforehand maybe accidentally by someone. When you delete a resource, there may be a short amount of time when the resource still appears in the portal but isn't actually available and that is why you are hitting the error because script was not able to find that resource. Another general perception about this error is that the Subscription you selected is the Subscription in which you actually have the resource since one single tenant might have number of subscription and sometime there might be confusion on same. Now assuming from your verbatim it seems like you have already checked it but will request you to check once more.
Now in case it is still not working let us do some testing. You can just try to create one test VM from scratch and then try to run same script as mentioned by you and see if it works for you or not because if it is working for me without any issue that rules out the possibility that script written by you is perfect for the work it is expected to do.
For Disk part still if you want to check the current scenario you can add the below script snippet and check if it works for you or not:
Get-AzDisk | where { $_.ManagedBy -eq $vm.Id } | Remove-AzDisk -Force
if ('DataDiskNames' -in $vm.PSObject.Properties.Name -and @($vm.DataDiskNames).Count -gt 0) {
Foreach ($uri in $vm.StorageProfile.DataDisks.Vhd.Uri) {
$dataDiskStorageAcct = Get-AzStorageAccount -Name $uri.Split('/')2.Split('.')[0]
$dataDiskStorageAcct | Remove-AzStorageBlob -Container $uri.Split('/')[-2] -Blob $uri.Split('/')[-1]
}
}
Did some digging and found below as well which you might like to test out just in case:
https://adamtheautomator.com/azure-vm-delete/
https://vautomation.dev/2020/10/azure/azure-delete-vm-and-child-resources-using-powershell/
Still I would recommend you to go ahead and test your script on a freshly created VM in your Subscription to see if it works for you.
Also there has been feedback as well here to actually bring this as an option on Portal. Currently When you delete a virtual machine (VM) in Azure, by default, any disks that are attached to the VM aren't deleted. This feature helps to prevent data loss due to the unintentional deletion of VMs.
Still if you want to investigate further into this I would recommend you to open a Support Case so they can check your environment and help you to figure out root cause. If you don't have any Support Plan do let me know I can help with one time free Support as well :)
Hope it helps!!!
Please "Accept as Answer" if it helped so it can help others in community looking for help on similar topics.