-
Notifications
You must be signed in to change notification settings - Fork 3
/
getMCACustomersFromBillingAccount.ps1
45 lines (32 loc) · 2.04 KB
/
getMCACustomersFromBillingAccount.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Retrieve all customers that your Billing Account has access to
# Authenticate to Azure
$azContext = Get-AzContext
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Tenant.Id)
$authHeader = @{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer ' + $token.AccessToken
}
$restUriBillingAccounts = "https://management.azure.com/providers/Microsoft.Billing/billingAccounts?api-version=2019-10-01-preview"
# Retreive the Billing Accounts you have access to
$restUriBillingAccountsResponse = Invoke-RestMethod -Uri $restUriBillingAccounts -Method Get -Headers $authHeader
# Filter Billing Account Name for the Microsoft Customer Agreement / Partner
$billingAccountName = ($restUriBillingAccountsResponse.value | Where-Object { $_.properties.agreementType -like "MicrosoftCustomerAgreement" }).name
Write-Host -ForegroundColor green "Retrieving customers for billing account" $billingAccountName
# Get Customers that your billing accoutn has access to
$restUriCustomers = "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/$billingAccountName/customers?api-version=2019-10-01-preview"
$restUriCustomersResponse = Invoke-RestMethod -Uri $restUriCustomers -Method Get -Headers $authHeader
$customers = @()
$customers += $restUriCustomersResponse.value
# get the nextLink and request content
$customersNextLink = $restUriCustomersResponse.Nextlink
# Do that magic until there is no more Nextlink received
while ($customersNextLink) {
$nextlinkResponse = Invoke-RestMethod -Uri $customersNextLink -method GET -Headers $authHeader
$customersNextLink = $nextlinkResponse.Nextlink
$customers += $nextlinkResponse.value
}
# Return the customer name
$customers.properties.displayName
Write-Host -foregroundcolor green "API Returned" $customers.count "customers"