运行下面的内容并不能使集合正确传递到ForEach循环,即使它返回我想要的名称。 $ RGInfo按预期返回。但是当我将它传递给Set-AzureRmResourceGroup的循环时,它的错误如下所示
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" }
}
}
知道我为什么一直得到以下内容?有四个RG'因此ForEach循环功能正常。
Set-AzureRmResourceGroup:' resourceGroupName'与预期不符 模式' ^ [ - \ w ._()] + $'。在行: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'与预期模式不匹配' ^ [ - \ w ._()] + $'。 在行: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'与预期模式不匹配' ^ [ - \ w ._()] + $'。 在行: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'与预期模式不匹配' ^ [ - \ w ._()] + $'。 在行:7 char:2 + Set-AzureRmResourceGroup -ResourceGroupName $ RGName -Tag @ {BUSINESS_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ + CategoryInfo:CloseError:(:) [Set-AzureRmResourceGroup],ValidationException + FullyQualifiedErrorId:Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet
答案 0 :(得分:0)
首先,您将$ RGInfo的内容过滤为仅资源组名称,您无法仅将名称添加到名称
$RGInfo = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "RG-CRP-D365*" } | Select-Object ResourceGroupName
下面的语法应该可行。
$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" }
}
}
希望这有帮助。
答案 1 :(得分:0)
我在从 CSV 文件导入列表时遇到了类似的问题。原来 RG 名称上有空格?♂️。