I have tried the below script throwing me an Error. But the Resource group still Exists
#Connect to Azure
Connect-AzAccount
#Specify the subscription ID
$subscriptionId = "xxxxxxxxxxxxxx"
#Get the list of virtual machines in the subscription
$vms = Get-AzVM -Status -ResourceGroupName "rg-aspera-dev-01"
#Loop through each virtual machine and remove the Microsoft Monitoring Agent extension
foreach ($vm in $vms) {
Check if the Microsoft Monitoring Agent extension is installed
if ($vm.Extensions | Where-Object { $.Publisher -eq "Microsoft.EnterpriseCloud.Monitoring" -and $.ExtensionType -eq "MicrosoftMonitoringAgent" }) {
Remove the Microsoft Monitoring Agent extension
Remove-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name "MicrosoftMonitoringAgent" -Force
}
}
ERROR:
ErrorCode: ResourceGroupNotFound
ErrorMessage: Resource group 'rg-aspera-dev-01' could not be found.
ErrorTarget:
StatusCode: 404
ReasonPhrase: Not Found
OperationID : 40634be7-1dd1-4aba-9049-280989ec523d