Public/Get-M365LicenseInventory.ps1

function Get-M365LicenseInventory {
    <#
    .SYNOPSIS
        Retrieves subscribed SKU inventory from Microsoft Graph.
    .OUTPUTS
        [PSCustomObject[]] One object per SKU.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$AccessToken
    )

    $skuFriendlyNames = @{
        'ENTERPRISEPREMIUM'           = 'Microsoft 365 E5'
        'ENTERPRISEPACK'              = 'Microsoft 365 E3'
        'SPE_E5'                      = 'Microsoft 365 E5'
        'SPE_E3'                      = 'Microsoft 365 E3'
        'SPE_F1'                      = 'Microsoft 365 F1'
        'MICROSOFT_365_COPILOT'       = 'Microsoft 365 Copilot'
        'FLOW_FREE'                   = 'Power Automate Free'
        'POWER_BI_STANDARD'           = 'Power BI Free'
        'POWER_BI_PRO'                = 'Power BI Pro'
        'PBI_PREMIUM_PER_USER'        = 'Power BI Premium Per User'
        'PROJECTPREMIUM'              = 'Project Plan 5'
        'VISIOCLIENT'                 = 'Visio Plan 2'
        'TEAMS_EXPLORATORY'           = 'Teams Exploratory'
        'STREAM'                      = 'Microsoft Stream'
        'EXCHANGESTANDARD'            = 'Exchange Online Plan 1'
        'EXCHANGEENTERPRISE'          = 'Exchange Online Plan 2'
        'Microsoft_Fabric_Free'       = 'Microsoft Fabric Free'
    }

    Write-M365Log "Retrieving subscribed SKUs from Microsoft Graph..."
    $skus = Invoke-M365GraphRequest -AccessToken $AccessToken -Uri "https://graph.microsoft.com/v1.0/subscribedSkus" -AllPages
    Write-M365Log "Retrieved $($skus.Count) SKUs"

    $ingestionTime = (Get-Date).ToUniversalTime().ToString('o')

    foreach ($sku in $skus) {
        $friendlyName = if ($skuFriendlyNames.ContainsKey($sku.skuPartNumber)) {
            $skuFriendlyNames[$sku.skuPartNumber]
        } else {
            $sku.skuPartNumber
        }

        $subscriptionIdsJson = if ($sku.subscriptionIds) {
            $sku.subscriptionIds | ConvertTo-Json -Compress
        } else { '[]' }

        [PSCustomObject]@{
            AccountId        = $sku.accountId
            AccountName      = $sku.accountName
            SkuId            = $sku.skuId
            SkuPartNumber    = $sku.skuPartNumber
            FriendlyName     = $friendlyName
            AppliesTo        = $sku.appliesTo
            CapabilityStatus = $sku.capabilityStatus
            ConsumedUnits    = $sku.consumedUnits
            TotalUnits       = $sku.prepaidUnits.enabled
            SuspendedUnits   = $sku.prepaidUnits.suspended
            WarningUnits     = $sku.prepaidUnits.warning
            LockedOutUnits   = $sku.prepaidUnits.lockedOut
            AvailableUnits   = $sku.prepaidUnits.enabled - $sku.consumedUnits
            SubscriptionIds  = $subscriptionIdsJson
            ServicePlans     = if ($sku.servicePlans) { ($sku.servicePlans | ConvertTo-Json -Compress -Depth 5) } else { "[]" }
            ServicePlanCount = if ($sku.servicePlans) { $sku.servicePlans.Count } else { 0 }
            IngestionTime    = $ingestionTime
        }
    }

    Write-M365Log "Emitted license inventory records to pipeline"
}