Get-CFAllLicenses.ps1

$Current = 0
$AllPrices = @()
if (-not $Customers) {
    $Customers = Get-CFCustomers -All -Verbose
}
$FilteredCustomers = $Customers | Where-Object { $_.externalServices.MICROSOFT -ne $null -and $_.externalServices.MICROSOFT -ne '' }

$AllLicenses = @()

foreach ($Customer in $FilteredCustomers) {
    $Current++
    Write-Host "Customer: $($Customer.name) - Tenant: $($Customer.externalServices.MICROSOFT) - Progress: $Current / $($FilteredCustomers.Count)" -ForegroundColor Cyan
    $Subscriptions = Get-CFMSSubscriptions -CustomerObject $Customer
    if ($Subscriptions) {
        foreach ($Subscription in $Subscriptions) {
            if ( -not $Subscription.autoRenewEnabled ) { continue }
            $CleanSKU = $Subscription.sku.Split(':')[0] + ':' + $Subscription.sku.Split(':')[1]
            #See if SKU is cached
            $CachedPrice = $AllPrices | Where-Object { $_.product.sku -eq $CleanSKU }

            if (-not $CachedPrice) {
                $PriceInfo = Get-CFProductPrice -FilterSKU $Subscription.sku
                if ($PriceInfo.results.Count -gt 0) {
                    $AllPrices += $PriceInfo.results
                }
            }

            [int]$BillingTerm = $Subscription.billingCycle -replace 'Annual', '12' -replace 'Monthly', '1'
            [int]$CommitmentTerm = $Subscription.termDuration -replace 'P1Y', 12 -replace 'P1M', 1

            $ProductId = ($AllPrices.product | Where-Object { $_.sku -eq $CleanSKU -and $_.billingTerm -eq $BillingTerm -and $_.recursionTerm -eq $CommitmentTerm }).id
            [double]$Price = ($AllPrices.price | Where-Object { $_.productId -eq $ProductId }).sale
            [double]$CostPrice = ($AllPrices.price | Where-Object { $_.productId -eq $ProductId }).cost
            
            [int]$CurrentQuantity = $Subscription.quantity
            [int]$RenewalQuantity = $Subscription.renewal.quantity

            if ($RenewalQuantity -eq 0 -and $RenewalQuantity -ne $CurrentQuantity) {
                $RenewalQuantity = $CurrentQuantity
            }

            $RenewalPrice = $Price * $RenewalQuantity
            $RenewalCostPrice = $CostPrice * $RenewalQuantity
            $CommitmentEndDate = (Get-Date $Subscription.commitmentEndDate).AddDays(1)
            $Year = $CommitmentEndDate.Year
            $Month = $CommitmentEndDate.Month
            $Day = $CommitmentEndDate.Day

            $newObj = [PSCustomObject]@{
                KL = $Customer.customerReference
                Customer = $Customer.name
                License = $Subscription.name
                Billing = $BillingTerm
                Commitment = $CommitmentTerm
                CurrentQuantity = $CurrentQuantity
                PricePerUnit = $Price
                RenewalQuantity = $RenewalQuantity
                RenewalPrice = $RenewalPrice
                RenewalCostPrice = $RenewalCostPrice
                Year = $Year
                Month = $Month
                Day = $Day
            }
            $AllLicenses += $newObj
        }
    }
}

$AllLicenses | Export-Excel -Path "C:\Temp\AllLicenses.xlsx" -AutoSize -WorksheetName "Licenses" -TableName "Licenses" #