Azure cmdlet: 如何使用Invoke-AzCostManagementQuery将结果分组?

huangapple go评论137阅读模式
英文:

Azure cmdlet: How to group Results with Invoke-AzCostManagementQuery?

问题

Microsoft已经引入了一个强大的cmdlet,名为Invoke-AzCostManagementQuery,它是Az.CostManagement模块的一部分。这使得可以生成各种维度的定制成本报告。

参数实际上相当简单:

  1. 要调查的范围
  2. 所需的时间段
  3. 成本的聚合
  4. 粒度
  5. 如果需要,根据规范进行分组

不幸的是,文档非常简陋,以至于即使在Microsoft的支持团队也难以发出正确的命令。

到目前为止,我已成功创建了以下查询:

首先,我设置了所需的范围:

$scope = "/subscriptions/xxxxxx"

其次,我定义了我的成本的聚合:

$aggregation = @{
  
  totalCost = @{
    name     = "Cost"
    function = "SUM"
  }
}

第三,我运行了这个命令:

Invoke-AzCostManagementQuery `
  -Scope $scope `
  -Timeframe 'DateToMonth' `
  -Type 'Usage' `
  -DatasetGranularity "Daily" `
  -DatasetAggregation $aggregation

我收到了我定义范围的每日成本:

Column                      Row
------                      ---
{Cost, UsageDate, Currency} {922,7007638124661 20230301 EUR, 944,695760723976 20230302 EUR, 870,8345694362154 20230303 EUR, 731,7461005886661 20230304 EUR…}

当我添加分组筛选器时:

$grouping = @{
  name      = "azcosts"
  type      = "TagKey"
}

我在调试日志中收到了期望的输出,但查询以错误结束,显示Invoke-AzCostManagementQuery: You cannot call a method on a null-valued expression.

Azure cmdlet: 如何使用Invoke-AzCostManagementQuery将结果分组?

英文:

Microsoft has introduced a powerful cmdlet called Invoke-AzCostManagementQuery, which is part of the Az.CostManagement module. This makes it possible to generate customized cost reports in all possible dimensions.

The parameters are (actually) quite simple:

 1. The scope to be surveyed
 2. the desired time period
 3. the aggregation of the costs
 4. the granularity
 5. if necessary, grouping according to specifications

Unfortunately, the documentation is so poor that even the support team at Microsoft has its problems issuing correct commands.

I was able to successfully create the following query until now:

First, I set the desired Scope:

$scope = "/subscriptions/xxxxxx"

Second, I define the aggregation of my costs:

$aggregation = @{
  
  totalCost = @{
    name     = "Cost"
    function = "SUM"
  }
}

Third, I run this command:

Invoke-AzCostManagementQuery `
  -Scope $scope `
  -Timeframe 'DateToMonth' `
  -Type 'Usage' `
  -DatasetGranularity "Daily" `
  -DatasetAggregation $aggregation

I receive the daily costs of my defined scope:

Column                      Row
------                      ---
{Cost, UsageDate, Currency} {922,7007638124661 20230301 EUR, 944,695760723976 20230302 EUR, 870,8345694362154 20230303 EUR, 731,7461005886661 20230304 EUR…}

When I add the Grouping Filter:

$grouping = @{
  name      = "azcosts"
  type      = "TagKey"
}

I receive the desired Output in the Debug Log, but the query ends up with an error Invoke-AzCostManagementQuery: You cannot call a method on a null-valued expression.

Azure cmdlet: 如何使用Invoke-AzCostManagementQuery将结果分组?

答案1

得分: 1

以下是翻译的部分:

要按资源上已定义的单个标记进行分组您可以像这样操作

$grouping = @{
  name      = "owner"
  type      = "TagKey"
}

$test = Invoke-AzCostManagementQuery `
    -Scope $scope `
    -Timeframe 'DateToMonth' `
    -Type 'Usage' `
    -DatasetGranularity "Daily" `
    -DatasetAggregation $aggregation `
    -DatasetGrouping $grouping
Return $test

在我的资源上有一个名为owner的标记

所以在我的情况下它返回类似以下内容

    Column   : {Cost, UsageDate, TagKey, TagValue}
    ETag     : 
    Id       : 
    Location : 
    Name     : 
    NextLink : 
    Row      : {64,45890538046535 20230301 owner mynster DKK, 62,92332096662609 20230302 owner mynster DKK, 63,05880866997392 20230303 owner mynster DKK, 63,04782384544536 20230304 owner mynster DKK}
    Sku      : 
    Tag      : Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20211001.ResourceTags
    Type     : 
    Property : Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20211001.QueryProperties

我无法确切地解释我是如何找到的但可以尝试

如果您转到Azure门户然后进入成本管理并配置查询一旦准备好执行查询请按 <kbd>F12</kbd> 并转到网络

在那里您将找到从您的计算机到网站的所有通信以及它运行的查询

它应该看起来类似于这样

[![在此输入图像描述][1]][1]

点击它然后转到Payload您可以在这里看到浏览器是如何构建结果的载荷就像这里显示的那样

[![在此输入图像描述][2]][2]

  [1]: https://i.stack.imgur.com/NE2ju.png
  [2]: https://i.stack.imgur.com/ys9H5.png

在这个示例中我正在查看名为"costcenter"的标记

并且还要补充一点如果您想要更多只需将分组放入一个数组中就像这样然后可以添加任意数量的分组

$grouping = @()
$grouping += @{
    name = "owner"
    type = "TagKey"
}
$grouping += @{
    name = "ResourceGroupName"
    type = "Dimension"
}
英文:

To group it by a single tag already defined on the resources you could do something like this:

  $grouping = @{
    name      = &quot;owner&quot;
    type      = &quot;TagKey&quot;
  }

  
$test = Invoke-AzCostManagementQuery `
    -Scope $scope `
    -Timeframe &#39;DateToMonth&#39; `
    -Type &#39;Usage&#39; `
    -DatasetGranularity &quot;Daily&quot; `
    -DatasetAggregation $aggregation `
    -DatasetGrouping $grouping
Return $test

On my ressources it has a tag called owner

So in my instance it returns something like this:

Column   : {Cost, UsageDate, TagKey, TagValue…}
ETag     : 
Id       : 
Location : 
Name     : 
NextLink : 
Row      : {64,45890538046535 20230301 owner mynster DKK, 62,92332096662609 20230302 owner mynster DKK, 63,05880866997392 20230303 owner mynster DKK, 63,04782384544536 20230304 owner mynster DKK…}
Sku      : 
Tag      : Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20211001.ResourceTags
Type     : 
Property : Microsoft.Azure.PowerShell.Cmdlets.CostManagement.Models.Api20211001.QueryProperties

I do not exactly have an easy way to explain how i found out but can try.

If you go to the azure portal and go to cost management and configure your query once you are ready to go ahead with your query press <kbd>F12</kbd> and go to Network.

In there you will find all communication from your PC to the website and the query it runs.

It should look something like this:

Azure cmdlet: 如何使用Invoke-AzCostManagementQuery将结果分组?

Press on it and go to Payload in here you can see how the browser constructed the payload for your result like shown here:

Azure cmdlet: 如何使用Invoke-AzCostManagementQuery将结果分组?

In this example i am looking at the Tag named "costcenter"

And just to add to this if you want more you can just put the grouping in an array like this and then add as many as you would like

$grouping = @()
$grouping += @{
    name = &quot;owner&quot;
    type = &quot;TagKey&quot;
}
$grouping += @{
    name = &quot;ResourceGroupName&quot;
    type = &quot;Dimension&quot;
}

huangapple
  • 本文由 发表于 2023年3月9日 17:55:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定