简体   繁体   中英

Azure remaining credit value from API

Whenever I log on the azure web portal i get a notification saying $xx.xx remaining credit .

I would like to understand how I can retrieve this value using Azure API, az cli or Powershell.

I have tried using the following as advised by the support but unfortunately without luck:

accesstoken=$(curl -s --header "accept: application/json" --request POST "https://login.windows.net/$TennantID/oauth2/token" --data-urlencode "resource=https://management.core.windows.net/" --data-urlencode "client_id=$ClientID" --data-urlencode "grant_type=client_credentials" --data-urlencode "client_secret=$ClientSecret" | jq -r '.access_token')

subscriptionURI="https://management.azure.com/subscriptions/$SubscriptionID?api-version=2016-09-01"

curl -s --header "authorization: Bearer $accesstoken" --request GET $subscriptionURI | jq .

The spending limit variable says just "on"

{
  "authorizationSource": "RoleBased",
  "subscriptionPolicies": {
  "spendingLimit": "On",
  redacted (too long)

When you see " $xx credit remaining ", it means your account has spending limit. There are many offerings which use spending limit mode. For example, you are given Azure Pass, or Microsoft Azure for Students Starter. Here is the list for reference ( https://azure.microsoft.com/en-us/support/legal/offer-details/ ).

In fact, Azure provides some REST APIs to let you work with Billing and Consumption but the API is limited to spending limit offerings. Advanced billing and consumption REST API are only fully supported for CSP (Cloud Service Provider) partner and Enterprise Agreement customers. That's said, with your current subscription, you cannot retrieve the balance summary (eg https://docs.microsoft.com/en-us/rest/api/billing/enterprise/billing-enterprise-api-balance-summary ).

However, there is a workaround to get usage details then perform SUM. To do so, first you need to retrieve billing period name https://management.azure.com/subscriptions/ {subscriptionId}/providers/Microsoft.Billing/billingPeriods?api-version=2017-04-24-preview

Here is the sample response of my billing periods:

  "value": [
    {
      "id": "/subscriptions/2dd8cb59-ed12-4755-a2bc-356c212fbafc/providers/Microsoft.Billing/billingPeriods/201805-1",
      "type": "Microsoft.Billing/billingPeriods",
      "name": "201805-1",
      "properties": {
        "billingPeriodStartDate": "2018-02-06",
        "billingPeriodEndDate": "2018-03-05"
      }
    },
    {
      "id": "/subscriptions/2dd8cb59-ed12-4755-a2bc-356c212fbafc/providers/Microsoft.Billing/billingPeriods/201804-1",
      "type": "Microsoft.Billing/billingPeriods",
      "name": "201804-1",
      "properties": {
        "billingPeriodStartDate": "2018-01-06",
        "billingPeriodEndDate": "2018-02-05"
      }
    },

...then get usage detail with the billing period name

https://management.azure.com/subscriptions/{Subscription}/providers/Microsoft.Billing/billingPeriods/201805-1?api-version=2017-04-24-preview

{
  "id": "subscriptions/2dd8cb59-ed12-4755-a2bc-356c212fbafc/providers/Microsoft.Billing/billingPeriods/201805-1/providers/Microsoft.Consumption/usageDetails/cdd74390-374b-53cf-2260-fc8ef10a2be6",
  "name": "cdd74390-374b-53cf-2260-fc8ef10a2be6",
  "type": "Microsoft.Consumption/usageDetails",
  "tags": null,
  "properties": {
    "billingPeriodId": "subscriptions/2dd8cb59-ed12-4755-a2bc-356c212fbafc/providers/Microsoft.Billing/billingPeriods/201805-1",
    "usageStart": "2018-02-06T00:00:00Z",
    "usageEnd": "2018-02-07T00:00:00Z",
    "instanceId": "/subscriptions/2dd8cb59-ed12-4755-a2bc-356c212fbafc/resourceGroups/securitydata/providers/Microsoft.Storage/storageAccounts/2d1993southeastasia",
    "instanceName": "2d1993southeastasia",
    "meterId": "c1635534-1c1d-4fc4-b090-88fc2672ef87",
    "usageQuantity": 0.002976,
    "pretaxCost": 7.1424E-05,
    "currency": "USD",
    "isEstimated": false,
    "subscriptionGuid": "2dd8cb59-ed12-4755-a2bc-356c212fbafc",
    "meterDetails": null
  }
},

This returns list of each resource's usage and cost in JSON format and you need to SUM all pretaxCost value. This way would be complicated with PowerShell because you need to initialize objects, then deserialize JSON and perform MATH. With PowerShell, it can be done technically but requires C# experience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM