How can i do consumption by location in Azure using az cli?

97 Views Asked by At

I am trying to do consumption by location using:

  az login
  $consumpCost = az consumption usage list --query "[? 
  type=='Microsoft.Consumption/usageDetails' && 
  instanceLocation=='northcentralus'].pretaxCost" --output json | ConvertFrom-Json
  $consumpCost = $consumpCost -split "`n" | ForEach-Object { [decimal]$_ }
  $combcost = $consumpCost | Measure-Object -Sum | Select-Object -ExpandProperty Sum
  Write-Host "Total Cost: $combcost"

But it always comes out 0.00 in total ie $combcost . Can anyone help as to why it is coming out as $0.00 ?

1

There are 1 best solutions below

8
Jahnavi On

I have removed the unnecessary lines of code such as split operator and place holders as it might lead to conflicts in displaying the correct results.

Modified Script:

$usageDetails = az consumption usage list --query "[?type=='Microsoft.Consumption/usageDetails' && instanceLocation=='US East'].pretaxCost" --output json | ConvertFrom-Json
$consumpCost = $usageDetails | ForEach-Object { [decimal]$_ }
$combcost = $consumpCost | Measure-Object -Sum | Select-Object -ExpandProperty Sum
Write-Host "Total Cost: $combcost"

Note: At times, if the Total Cost appears as 0, it doesn't necessarily indicate an issue with the results. It could be that the specified location in your query is not actively used by any users or applications within the particular enterprise agreement. In such scenarios, consider trying other locations to validate the details.

After I executed the above code in my environment, I was able to retrieve the cost consumption details successfully.

Output:

enter image description here

enter image description here