0
votes

Running this below and can't seem to get the collection correct to pass into the ForEach loop, even though it returns the names I want. $RGInfo returns as expected. But when I pass it into the loop to Set-AzureRmResourceGroup, it errors as below

PS H:\> $rginfo

ResourceGroupName   
-----------------   
rg-crp-d365-bp-n    
rg-crp-d365-dev1-n  
rg-crp-d365-dev2-n  
rg-crp-d365-upgrad-n

$RGInfo = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "RG-CRP-D365*" } | Select-Object ResourceGroupName

ForEach ($RGName in $RGInfo) {
    If ($RGName.Tags -eq $null) { 
        Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag @{BUSINESS_UNIT="CRP"; COST_CENTER="6435" }       
    }
}

Know why I keep getting the below? There are four RG's so the ForEach loop is functional.

Set-AzureRmResourceGroup : 'resourceGroupName' does not match expected pattern '^[-\w._()]+$'. At line:7 char:2 + Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag @{BUSINESS_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet Set-AzureRmResourceGroup : 'resourceGroupName' does not match expected pattern '^[-\w._()]+$'. At line:7 char:2 + Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag @{BUSINESS_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet Set-AzureRmResourceGroup : 'resourceGroupName' does not match expected pattern '^[-\w._()]+$'. At line:7 char:2 + Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag @{BUSINESS_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet Set-AzureRmResourceGroup : 'resourceGroupName' does not match expected pattern '^[-\w._()]+$'. At line:7 char:2 + Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag @{BUSINESS_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet

2
If ($RGName.Tags -eq $null) - this check doesnt make sense as you are not passing tags, so its always true. as for the error i have no clue, try debugging. or do something like: $rgname right before set-azurermresourcegroup4c74356b41

2 Answers

0
votes

First you filtered the content of $RGInfo to only the Resource Group name, you cannot add tags to just the name

$RGInfo = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "RG-CRP-D365*" } | Select-Object ResourceGroupName

Below syntax should work.

$RGInfo = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "RG-CRP-D365*" }

ForEach ($RGName in $RGInfo)
{
    If ($RGName.Tags -eq $null)
{ 
Set-AzureRmResourceGroup -ResourceGroupName $RGName.ResourceGroupName -Tag @{BUSINESS_UNIT="CRP"; COST_CENTER="6435" }       
    }
}

Hope this helps.

0
votes

I had a similar issue when importing a list from a CSV file. Turns out the RG names had spaces on it 🤦‍♂️.