英文:
Azure cmdlet: How to group Results with Invoke-AzCostManagementQuery?
问题
Microsoft已经引入了一个强大的cmdlet,名为Invoke-AzCostManagementQuery
,它是Az.CostManagement
模块的一部分。这使得可以生成各种维度的定制成本报告。
参数实际上相当简单:
- 要调查的范围
- 所需的时间段
- 成本的聚合
- 粒度
- 如果需要,根据规范进行分组
不幸的是,文档非常简陋,以至于即使在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.
英文:
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.
答案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 = "owner"
type = "TagKey"
}
$test = Invoke-AzCostManagementQuery `
-Scope $scope `
-Timeframe 'DateToMonth' `
-Type 'Usage' `
-DatasetGranularity "Daily" `
-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:
Press on it and go to Payload in here you can see how the browser constructed the payload for your result like shown here:
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 = "owner"
type = "TagKey"
}
$grouping += @{
name = "ResourceGroupName"
type = "Dimension"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论