Some information is only accessible through the GUI and not by API or abstracted away in a PowerShell module. I got the question if it was possible to be able to get the partner details shown on https://admin.microsoft.com/Adminportal/Home#/partners through code. This is just one of those cases where you can’t have the information in a supported way, but there is a way.

This small piece of code takes a username and password of a global admin account. It uses Connect-AzAccount from the Az.Accounts module to connect to Azure to get a token. The token is used to get another token with a different audience (“https://admin.microsoft.com”) to be able to get the information from the Microsoft 365 portal.

Was a bit surprised myself to see it works to just throw a bearer token along in the header as I don’t see that happening when browsing through the pages and analyzing the traffic.

The code only works in PowerShell Core. In PowerShell 5.1 you’ll get a message from Invoke-RestMethod about the value size of the cookie being too big for its configured maximum size. If anyone knows how to change this size, or work around this error in another way, please let me know.

$userName = "abc@xyz.onmicrosoft.com" 
$password = 'P@$$w0rd' 

$secStringPassword = ($password | ConvertTo-SecureString -AsPlainText -Force)
$credential = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)

$tenantId = (Invoke-RestMethod "https://login.windows.net/$($userName.Split("@")[1])/.well-known/openid-configuration" -Method GET).userinfo_endpoint.Split("/")[3]
$uri = "https://admin.microsoft.com/fd/commerceMgmt/partnermanage/partners?customerTenantId=$($tenantId)&api-version=2.1"

$resource = "https://admin.microsoft.com"

$azContext = (Connect-AzAccount -Credential $credential -ErrorAction Stop).Context
$token =
    [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate(
        $azContext.Account,
        $azContext.Environment,
        $azContext.Tenant.Id.ToString(),
        $null,
        [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never,
        $null,
        $resource
    ).AccessToken
$null = Disconnect-AzAccount -AzureContext $azContext

$s = Invoke-RestMethod -Method GET -Uri $uri -Headers  @{ "Content-Type"  = "application/json" ; "Authorization" = "Bearer $token" } 
$s.partners

Thanks Jos Lieben for borrowing line 7 ;)