I have a powershell script that I am trying to use to destroy a subscriptions resource groups based on the environment.
However I seem to get the following error everytime I try to get the resource names into a variable as objects.
# Set the variables here
$_subscription = "Test Data Platform"
$_environment = "prd" #prd, dev, uat
Connect-AzAccount -Subscription $_subscription
Write-Host ("The acquired list of resource group names is as below:")
$rgs=Get-AzResourceGroup -Tag @{'environment'=$_environment} | Select-Object -Property ResourceGroupName
$rgs | ForEach-Object -Parallel {
$r = Get-AzResourceGroup -Name $_ -Tag @{'environment'=$_environment} -ErrorAction SilentlyContinue
if ($r) {
$message = "We have a resource group called {0}" -f $_
Write-PSFMessage $message -Level Output
try {
$message = "### Removing resource group {0}" -f $_
Write-PSFMessage $message -Level Output
Remove-AzResourceGroup -Name $_ -Confirm:$false -Force | Out-Null
$message = "### Resource group {0} Removed" -f $_
Write-PSFMessage $message -Level Output
}
catch {
$message = "### FAILED - REMOVING -Resource group {0}" -f $_
Write-PSFMessage $message -Level Significant
}
}
else {
$message = "There is no resource group called {0}" -f $_
Write-PSFMessage $message -Level Output
}
} -ThrottleLimit 25
Error
ForEach-Object : Parameter set cannot be resolved using the specified named parameters.
At C:\Test\destroy-dev-az.ps1:10 char:8
+ $rgs | ForEach-Object -Parallel {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [ForEach-Object], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCommand
Run it in Powershell 7 instead and got the following error:
The acquired list of resource group names is as below:
[15:44:46][<ScriptBlock>] There is no resource group called @{ResourceGroupName=Test2}
[15:44:47][<ScriptBlock>] There is no resource group called @{ResourceGroupName=Test1}
UPDATE
Resolved the above error by changing $rgs to $rgs.ResourceGroupName.


