Azure Subscription metadata

Did you ever come accross unkown subscriptions in your tenant? Some employee who started a free trial with their company creds or perhaps a student who upgraded his/her Azure for Students subscription? The billing info is retrievable in the Azure portal by browsing to the subscription and going to either the Billing Properties blade (Azure Plan), or the Properties (legacy) blade. The billing account name is usually the name of the person who created the subscription.

There is no official api supported by Microsoft to retrieve this metadata. If you do need to use code, feel free to see if this might help you out. Please note this is very much not supported.

 1$authHeader = @{
 2    'Content-Type'  = 'application/json'
 3    'Authorization' = 'Bearer ' + (Get-AzAccessToken).Token
 4}
 5
 6# Change the azure Resource Graph Query to your liking to get the subscriptions you need. The targeted management group could be the Default management group for new subscriptions.
 7
 8$subscriptions = Search-AzGraph -Query @"
 9resourcecontainers
10| where type == "microsoft.resources/subscriptions"
11| where (properties.managementGroupAncestorsChain[0].name) == 'my management group'
12| where properties.subscriptionPolicies.quotaId != 'AzureForStudents_2018-01-01'
13| where properties.subscriptionPolicies.quotaId != 'MSDN_2014-09-01'
14| project id,name,subscriptionId,(properties.managementGroupAncestorsChain[0].name),(properties.state),(properties.subscriptionPolicies.quotaId)
15"@
16
17if ( $($subscriptions.count) -lt 1 ) { exit }
18
19$subscriptionInfo = @()
20
21Foreach ($subscription in $subscriptions)
22{
23    $subscriptionResponse = ''
24
25    $uri = 'https://management.azure.com/subscriptions/{0}/providers/Microsoft.Billing/billingProperty/default?api-version=2019-10-01-preview' -f $subscription.subscriptionId
26
27    try {
28        $subscriptionResponse = Invoke-RestMethod -Uri $uri -Method Get -Headers $authHeader
29    } catch {
30        # an error is usually thrown when it is a legacy subscription. Lets try again with the isLegacy parameter to true.
31
32        $uri = 'https://management.azure.com/subscriptions/{0}/providers/Microsoft.Billing/billingProperty/default?api-version=2019-10-01-preview&islegacy=true' -f $subscription.subscriptionId
33        try {
34            $subscriptionResponse = Invoke-RestMethod -Uri $uri -Method Get -Headers $authHeader
35        } catch {
36            Write-Error "Exception Response: " $_.Exception.Response
37        }
38    }
39
40    $subscriptionInfo += $subscriptionResponse | ConvertTo-Json -Depth 10
41
42}