iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 25
0
Microsoft Azure

深入 Azure 雲端服務系列 第 24

Azure CLI + PowerShell 分析成本花費 - 2

例子 : 查詢一段時間服務類型為組的金額合計花費

邏輯 :

  1. 因為 meterCategory 是子屬性 (nested property) 所以 ConvertFrom-Json Group 無法簡單 Group,所以使用 JMESPath Select meterCategory 到從子屬性移到主屬性就可以
  2. 搭配 Measure-Object 做群組彙總的計算
$json = az consumption usage list -m `
    --query "[].{meterCategory:meterDetails.meterCategory,consumedService:consumedService,pretaxCost:pretaxCost,usageStart:usageStart}";
$g1 = $json | ConvertFrom-Json | %{[PSCustomObject]$_} | Group consumedService , meterCategory ;
$g2 = $g1 | %{
    New-Object psobject -Property @{
        meterCategory = $_.Name
        count = $_.count
        pretaxCost = ($_.Group | Measure-Object pretaxCost -Sum).Sum
        minDate = ($_.Group | Measure-Object usageStart -Minimum).Minimum
        maxDate = ($_.Group | Measure-Object usageStart -Maximum).Maximum
    }
}

foreach ($item in $g2) {
    Write-Host($item);
}

結果 :

@{maxDate=2020-10-09T00:00:00Z; minDate=2020-09-26T00:00:00Z; meterCategory=Microsoft.Storage, Storage; count=110; pretaxCost=0.005666577}
@{maxDate=2020-10-03T00:00:00Z; minDate=2020-09-26T00:00:00Z; meterCategory=microsoft.web, Azure App Service; count=43; pretaxCost=19.26665616}
@{maxDate=2020-10-03T00:00:00Z; minDate=2020-10-02T00:00:00Z; meterCategory=Microsoft.ContainerInstance, Container Instances; count=4; pretaxCost=1.0868580843126}
@{maxDate=2020-10-09T00:00:00Z; minDate=2020-09-30T00:00:00Z; meterCategory=Microsoft.Compute, Bandwidth; count=17; pretaxCost=5.84E-06}
@{maxDate=2020-10-03T00:00:00Z; minDate=2020-10-02T00:00:00Z; meterCategory=Microsoft.DBforPostgreSQL, Azure Database for PostgreSQL; count=4; pretaxCost=5.99465829032258}
@{maxDate=2020-10-09T00:00:00Z; minDate=2020-09-30T00:00:00Z; meterCategory=Microsoft.Compute, Storage; count=11; pretaxCost=1.3559448279}
@{maxDate=2020-10-01T00:00:00Z; minDate=2020-09-27T00:00:00Z; meterCategory=Microsoft.ContainerRegistry, Container Registry; count=33; pretaxCost=12.0277949008}
@{maxDate=2020-10-09T00:00:00Z; minDate=2020-09-30T00:00:00Z; meterCategory=Microsoft.Compute, Virtual Machines; count=10; pretaxCost=15.4138090088}
@{maxDate=2020-10-09T00:00:00Z; minDate=2020-09-26T00:00:00Z; meterCategory=Microsoft.Storage, Bandwidth; count=22; pretaxCost=0.00792072}
@{maxDate=2020-10-09T00:00:00Z; minDate=2020-09-27T00:00:00Z; meterCategory=Microsoft.Network, Virtual Network; count=18; pretaxCost=1.42639722222222}
@{maxDate=2020-10-03T00:00:00Z; minDate=2020-10-02T00:00:00Z; meterCategory=microsoft.web, Bandwidth; count=3; pretaxCost=0}

上一篇
Azure CLI + PowerShell 分析成本花費
下一篇
Azure Retail Prices REST API 分析全 Azure 服務價格差異 - JSON 轉 Excel版本
系列文
深入 Azure 雲端服務29

尚未有邦友留言

立即登入留言