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.
CodePudding user response:
As Santiago Squarzon has mentioned ForEach-Object -Parallel was introduced in PowerShell 7.0 Preview 3 , so If you are using Powershell ISE it will be of Version 5.0. So , You will get error as below :
If you run in Powershell 7 , then the issue is that the command you are using to get resource group names by using select-object returns output with a CRLF endings and using that as a parameter in another command will give you below errors:
ERROR: Parameter 'resource_group_name' must conform to the following pattern: '^[-\\w\\._\\(\\)] $'.
OR
So , inorder to solve that you will need to get the resource names with this command $rgs=(Get-AzResourceGroup).ResourceGroupName. so your over all code will be like below :
# 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}).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
Output:



