I am trying to get my azure subscription current cost using PowerShell.
Desired Output:
e.g:
currency = GBP
current cost = 370.74
Get-AzConsumptionUsageDetail -BillingPeriodName 202105
But this does not give me the desired output.
I am trying to get my azure subscription current cost using PowerShell.
Desired Output:
e.g:
currency = GBP
current cost = 370.74
Get-AzConsumptionUsageDetail -BillingPeriodName 202105
But this does not give me the desired output.
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
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
Can you see if this works for you:
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.
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.