【问题标题】:Consolidating and editing Azure Tags in bulk with no data loss or outage?在没有数据丢失或中断的情况下批量整合和编辑 Azure 标记?
【发布时间】:2020-10-23 13:03:07
【问题描述】:

如何纠正以下拼写错误或有空格或拼写错误的 Azure 标记?

有没有什么脚本可以把所有的标签编辑替换成正确的关键字https://docs.microsoft.com/en-us/powershell/module/az.resources/get-aztag?view=azps-4.8.0

来自:

  Name        Count
  Cost-Center    750
  Cost-Centre    250
  Cost Center    250
  Cost Centre    500

进入:

   Name            Count
   Cost Center     1750

最好是散装,这样就不需要手动检查所有对象并一个一个地手动替换它。

【问题讨论】:

    标签: azure powershell azure-powershell


    【解决方案1】:

    这可能非常棘手,您必须仔细确定要查找的拼写错误才能将名称更改为有效的。

    可能是这样的

    # testdata
    $azTags = @"
    Name,Count
    Cost-Center,750
    Cost-Centre,250
    Cost Center,250
    Cost Centre,500
    Department,5
    Dept,15
    "@ | ConvertFrom-Csv
    
    # find misspelled names using wildcards and replace with hardcoded correct name
    $azTags | Where-Object { $_.Name -like 'Cost*Cent*' } | ForEach-Object { $_.Name = 'CostCenter' }
    $azTags | Where-Object { $_.Name -like 'Dep*' } | ForEach-Object { $_.Name = 'Department' }
    
    # group the objects and calculate the sum of all Count properties
    $azTags | Group-Object Name | ForEach-Object {
        [PsCustomObject]@{
            Name = $_.Name
            Count = ($_.Group | Measure-Object -Property Count -Sum).Sum
        }
    }
    

    输出

    Name       Count
    ----       -----
    CostCenter  1750
    Department    20
    

    也许更好的方法是使用包含正确拼写的键和一个正则表达式字符串作为值构建一个哈希表,该字符串试图匹配您遇到的所有错误拼写。 (如上使用带有通配符的-like 可能太粗略了)

    当然,您必须首先获取所有坏名称的列表,并从中找出正则表达式应该是什么才能捕获它们。

    例如,您可以这样:

    Name                Count
    ----                -----
    Cost-Center           750
       Cost-Centre        250  
    Cost Center           250
    Cost   Centre         500
    Department              5
     Dept                  15
    Dpt.                    1
    IT                      3
      I.T.                  6
    Information Technology  4
    

    那么下面可能会清理所有yang 并用yin 替换它

    # testdata
    $azTags = @"
    Name,Count
    Cost-Center,750
       Cost-Centre,250  
    Cost Center,250
    Cost   Centre,500
    Department,5
     Dept,15
    Dpt.,1
    IT,3
      I.T,6
    Information Technology,4
    "@ | ConvertFrom-Csv
    
    # create a hashtable of correct names (yin) and a regex string containing bad matches (yang)
    $hash = @{
        'CostCenter' = '^\s*Cost[^C]+Cent[re]\s*'
        'Department' = '^\s*(Dep[^a]+|Dpt\.?)\s*'
        'IT'         = '^\s*(I\.?T\.?|Information[^T]+)\s*'
    }
    
    # loop through the keys and replace the items in $azTags that match the yang
    foreach ($yin in $hash.Keys) {
        $azTags | Where-Object { $_.Name -match $hash[$yin] } | ForEach-Object { $_.Name = $yin }
    }
    

    完成后,您可以按名称对项目进行分组并获得 Count 值的总数:

    # group the objects and calculate the sum of all Count properties
    $azTags | Group-Object Name | ForEach-Object {
        [PsCustomObject]@{
            Name = $_.Name
            Count = ($_.Group | Measure-Object -Property Count -Sum).Sum
        }
    }
    

    输出:

    Name       Count
    ----       -----
    CostCenter  1750
    Department    21
    IT            13
    

    【讨论】:

    • 如果yang在关键字的前面或后面包含空格怎么办?
    • @SeniorSystemsEngineer 我添加了新代码。希望这能更好地满足您的需求
    • 感谢您的支持,所以我假设在这种情况下我可以使用:Update-AzTag -ResourceId /subscriptions/{subId} -Tag $azTags -Operation Merge 根据您的上述脚本和此命令:docs.microsoft.com/en-us/powershell/module/az.resources/…
    • @SeniorSystemsEngineer 我猜是这样,但我无法测试它。要非常小心,尝试将它放在测试集上,以确保你不会破坏任何重要的东西......
    猜你喜欢
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2011-11-03
    • 2018-10-30
    • 2020-11-26
    • 2017-02-04
    相关资源
    最近更新 更多