Rules/TEN-001.unused-licenses.psd1

@{
    Id          = 'TEN-001'
    Title       = 'No significant share of paid licenses is unused'
    Area        = 'TenantBasics'
    Severity    = 'Info'
    References  = @(
        'https://learn.microsoft.com/microsoft-365/commerce/licenses/subscriptions-and-licenses'
    )
    Rationale   = 'Unassigned paid licenses are recurring spend without value - and in security terms, an unassigned E3/Business Premium seat often means a user who is missing the protection you already paid for.'
    Remediation = 'Reassign or reduce the flagged license counts at the next renewal; check whether unlicensed active users should hold one of the free seats.'
    Test        = {
        param($Snapshot)
        $flagged = [System.Collections.Generic.List[string]]::new()
        foreach ($sku in @($Snapshot.TenantBasics.skus)) {
            if (-not ($sku.prepaidUnits -and $sku.prepaidUnits.PSObject.Properties['enabled'])) { continue }
            $enabled = [int]$sku.prepaidUnits.enabled
            $consumed = [int]$sku.consumedUnits
            # ignore tiny pools and viral/free SKUs with huge synthetic caps
            if ($enabled -lt 5 -or $enabled -gt 5000) { continue }
            $unused = $enabled - $consumed
            if ($unused -ge 5 -and ($unused / [double]$enabled) -ge 0.2) {
                $flagged.Add(("{0}: {1} of {2} licenses unused" -f $sku.skuPartNumber, $unused, $enabled))
            }
        }
        if ($flagged.Count -gt 0) {
            New-TLRuleResult -Status Fail -Evidence @($flagged)
        }
        else {
            New-TLRuleResult -Status Pass -Evidence 'No paid SKU has a significant unused share.'
        }
    }
}