1

I am trying to get my azure subscription current cost using PowerShell. enter image description here

Desired Output:

e.g:

currency = GBP

current cost = 370.74

Get-AzConsumptionUsageDetail -BillingPeriodName 202105

But this does not give me the desired output.

rAJ
  • 1,295
  • 5
  • 31
  • 66
  • Does this answer your question? [azure billing REST API](https://stackoverflow.com/questions/63351588/azure-billing-rest-api) – Peter Bons Jun 03 '21 at 14:30
  • No, this output is very complex. I want simply the overall current cost of the subscription. – rAJ Jun 03 '21 at 14:34

3 Answers3

2

The below query will give you the cost of the mentioned month. e.g: cost of May month

Get-AzConsumptionUsageDetail -BillingPeriodName 202105

But if your subscription does not start with the 1st of May then in this case the above query will give you the wrong cost. e.g: your billing cycle is from 10 May to 09 June

To know the cost of your exact billing cycle you need to get the current billing period first and then get the cost based on billing cycle start/end date.

$currentBillingPeriod = Get-AzBillingPeriod -MaxCount 1
$startDate = $currentBillingPeriod.BillingPeriodStartDate.ToString("dd-MM-yyyy")
Write-Host "currentBillingPeriod startDate : " $startDate
$endDate = $currentBillingPeriod.BillingPeriodEndDate.ToString("dd-MM-yyyy")
Write-Host "currentBillingPeriod endDate : " $endDate

$currentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate | Measure-Object -Property PretaxCost -Sum
Write-Host "Current Cost of Subscription : " $currentCost.Sum
rAJ
  • 1,295
  • 5
  • 31
  • 66
1

Try the following powershell script:

$tenantId="76a1f773...b-86b9-d1ced3e15cda"
$clientId="0159ec7d-f...-a680-c4d40ab7a36c"
$clientSecret="o4eq4jj...I26uz26W~"
$secSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force

$pscredential = New-Object System.Management.Automation.PSCredential ($clientId, $secSecret)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

$dexResourceUrl="https://management.azure.com/"
$context = Get-AzContext
$token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $dexResourceUrl).AccessToken


$SubscriptionId = '3465e081-85b6-4b54-a3e1-15675acb615f'
$billingperiod = '202010-1'

#Create the REST-URL
$usageURL ="https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Billing/billingPeriods/$billingperiod/providers/Microsoft.Consumption/usageDetails?api-version=2017-11-30"

$header = @{
    'Authorization' = "Bearer $($token)"
    "Content-Type" = "application/json"
}
 
$UsageData = Invoke-RestMethod `
    -Method Get `
    -Uri $usageURL `
    -ContentType application/json `
    -Headers $header 

ConvertTo-Json $UsageData

source: https://stackoverflow.com/a/63353164/1384539

rAJ
  • 1,295
  • 5
  • 31
  • 66
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • Thanks for the reply. This gives me resource-wise data. Not exactly what I want. I don't think Microsoft exposed any API or command which can give directly the cost of the subscription. – rAJ Jun 03 '21 at 14:31
  • yes, I have the same feeling. You'd better query the REST Api – Thiago Custodio Jun 03 '21 at 14:57
0

Can you see if this works for you:

  1. Try AzConsumptionUsageDetail -BillingPeriodName <periodname> | Measure-Object PretaxCost -Sum. Where period name is in the format 20210601

EDIT: Realised I missed the obvious in your post and you've already tried that - I'm leaving it here in case someone else might still find it helpful to someone else.

  1. Else set a budget for all Subscriptions, then use Get-AzConsumptionBudget.

See this post where it was asked recently and with the help of OP, we were able to get much closer to what he needed.

Ked Mardemootoo
  • 1,480
  • 1
  • 5
  • 19
  • Thanks i missed 'Measure-Object PretaxCost -Sum'. It gives me current cost what i wanted. Although it is not giving me the currency type information. e.g: GBP or INR. Any idea if it is also possible ? – rAJ Jun 04 '21 at 06:01
  • 1
    Otherwise i will get it from my current query result where i have detailed output including currency. No issues. – rAJ Jun 04 '21 at 06:19
  • I couldn't find a way to get the currency unfortunately but yes like you said getting it from the results would be easier - if you don't mind please share how you get the currency once you have the right filter/format output query – Ked Mardemootoo Jun 04 '21 at 07:08
  • 1
    $consumption = Get-AzConsumptionUsageDetail -BillingPeriodName $billingPeriod -Top 1 Write-Host "Subscription Currency : " $consumption.Currency – rAJ Jun 04 '21 at 07:45