Powershell在foreach中使用时获得相同的值

时间:2018-08-11 07:58:00

标签: azure powershell

我正在尝试从Azure到Zabbix获取指标。

问题在于,Metric for VM由2个单词组成:Percentage CPU,而Zabbix不允许项目键由2个单词组成。我还尝试了Percentage%20CPU,但是在Zabbix中遇到了错误,并且创建了Zabbix密钥percentage_cpu

因此,我决定先将数据从Zabbix发送到Azure,然后将percentage_cpu转换为Percentage%20CPU。如果仅存在该键,则效果很好,但是当我添加另一个键(在此示例中为SQL指标)时,问题就开始了。

对于SQL度量标准,所有值都在一个词中-无需更改任何内容,但是VM的度量标准也已分配给SQL。我试图避免为每个服务编写单独的文件

     $host_items = Get-ZabbixHostItems -url $zabbix_url -auth $zabbix_auth - 
 zabbix_host $host_name  
    foreach ($host_item in $host_items) 

    { 

        #$host_item_details = select-string -InputObject $host_item.key_ -Pattern '^(azure\.sql)\.(.*)\.(.*)\[\"(.*)\"\]$'; 

        $host_item_details = select-string -InputObject $host_item.key_ -Pattern '^(azure\.\w{2,})\.(.*)\.(.*)\[\"(.*)\"\,(.*)]$'; 

        #$host_item_details = select-string -InputObject $host_item.key_ -Pattern '^(azure)\.(.*)\.(.*)\.(.*)\[\"(.*)\"\,(.*)]$'; 

        $host_item_provider = $host_item_details.Matches.groups[1]; 

        $host_item_metric = $host_item_details.Matches.groups[2]; 

        $host_item_timegrain = $host_item_details.Matches.groups[3]; 

        $host_item_resource = $host_item_details.Matches.groups[4]; 

        $host_item_suffix = $host_item_details.Matches.groups[5]; 


         if ($host_item_metric='percentage_cpu')
          {$host_item_metric='Percentage%20CPU'}
          else

          { $host_item_metric = $host_item_details.Matches.groups[2];}
      #}



        $uri = "https://management.azure.com{0}/providers/microsoft.insights/metrics?api-version={1}&interval={2}&timespan={3}&metric={4}" -f ` 

            $host_item_resource, ` 

            "2017-05-01-preview", ` 

            $host_item_timegrain.ToString().ToUpper(), ` 

            $($(get-date).ToUniversalTime().addminutes(-15).tostring("yyyy-MM-ddTHH:mm:ssZ") + "/" + $(get-date).ToUniversalTime().addminutes(-2).tostring("yyyy-MM-ddTHH:mm:ssZ")), ` 


            $host_item_metric; 


        write-host $uri; 
      }

hostitems的输出_

azure.sql.dtu_consumption_percent.pt1m["/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Sql/servers/mojsql/databases/test",common]
azure.vm.percentage_cpu.pt1m["/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/test",common]

当我在上面运行代码时,我得到了这些URI

https://management.azure.com/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Sql/servers/mojsql/databases/test/providers/microsoft.insights/m
etrics?api-version=2017-05-01-preview&interval=PT1M&timespan=2018-08-11T07:38:05Z/2018-08-11T07:51:05Z&metric=Percentage%20CPU


https://management.azure.com/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/test/providers/microsoft.insights/metric
s?api-version=2017-05-01-preview&interval=PT1M&timespan=2018-08-11T07:38:05Z/2018-08-11T07:51:05Z&metric=Percentage%20CPU

对于第一个链接(SQL)指标应为dtu_consumption,但对于两个链接我都得到相同的指标

第二次尝试:

if ($host_item_metric -eq 'percentage_cpu')
          {$host_item_metric='Percentage%20CPU';}
          else

          { $host_item_metric = $host_item_details.Matches.groups[2];}

      write-host $host_item_metric
    }

输出:(原始值)

dtu_consumption_percent
percentage_cpu

1 个答案:

答案 0 :(得分:0)

必须使用-like

if ($host_item_metric -like 'percentage_cpu')
          {$host_item_metric='Percentage%20CPU';}
          else

          { $host_item_metric = $host_item_details.Matches.groups[2]}