LunaM365 Lizenz-ZwillingEin Produkt von Luna · Wechselcheck für Funktionsverluste

Lese-Skript (nur lesen)

Das Klartextskript nutzt einen bereits bestehenden delegierten Microsoft-Graph-Kontext. Es liest mit Get-Befehlen und schreibt eine lokale Datei. Tenant-, Benutzerobjekt- und Gruppen-IDs sind pseudonymisiert; UPNs bleiben für den lokalen Abgleich erhalten. Die Ausgabe ist vertraulich. Das Skript ändert keine Lizenzen und sendet nichts an uns.

Skript herunterladen Anleitung

#Requires -Version 7.0
<#
M365 Lizenz-Zwilling: schreibgeschützter Microsoft-Graph-Export. Unabhängiges Werkzeug.
Module: Microsoft.Graph.Authentication, Microsoft.Graph.Users,
Microsoft.Graph.Identity.DirectoryManagement.
Vorher selbst einen bestehenden delegierten Graph-Kontext im richtigen Tenant herstellen.
Delegierte Scopes: User.Read.All und LicenseAssignment.Read.All. Keine Write-Scopes.
Nur Get-MgContext, Get-MgUser und Get-MgSubscribedSku greifen auf den Kontext/Graph zu.
Schreibt eine neue lokale Datei: lizenzzwilling-inventar.json. Kein Upload.
UPNs sind für den lokalen Join nötig und bleiben im Klartext. Datei vertraulich behandeln.
Tenant-, Benutzerobjekt- und Gruppen-IDs sind pro Export pseudonymisiert; öffentliche
SKU-/Dienstplan-IDs bleiben unverändert. Das Inventar weist keine Dienstnutzung nach.
#>
[CmdletBinding()]
param([string]$Ausgabe = './lizenzzwilling-inventar.json')
$ErrorActionPreference = 'Stop'
$ctx = Get-MgContext
if (-not $ctx -or $ctx.AuthType -ne 'Delegated' -or -not $ctx.TenantId) {
    throw 'Kein geeigneter delegierter Graph-Kontext vorhanden.'
}
foreach ($scope in @('User.Read.All', 'LicenseAssignment.Read.All')) {
    if ($ctx.Scopes -notcontains $scope) { throw "Delegierter Scope fehlt: $scope" }
}
if (@($ctx.Scopes | Where-Object { $_ -match '(?i)write|fullcontrol|manage' }).Count -gt 0) {
    throw 'Der Graph-Kontext enthält weitergehende Berechtigungen. Einen reinen Lesekontext verwenden.'
}
$full = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Ausgabe)
if ([IO.File]::Exists($full) -or [IO.Directory]::Exists($full)) { throw 'Ausgabepfad existiert bereits. Einen neuen Pfad wählen.' }
$collected = [DateTime]::UtcNow.ToString('o')
$key = [Security.Cryptography.RandomNumberGenerator]::GetBytes(32)
$mac = [Security.Cryptography.HMACSHA256]::new($key)
function Get-Pseudonym([string]$Kind, [string]$Value) {
    if ([string]::IsNullOrWhiteSpace($Value)) { return $null }
    $inputBytes = [Text.Encoding]::UTF8.GetBytes($Kind + ':' + $Value.Trim().ToLowerInvariant())
    return [Convert]::ToHexString($mac.ComputeHash($inputBytes)).ToLowerInvariant()
}
try {
    $users = @(Get-MgUser -All -Property Id,UserPrincipalName,UserType,AccountEnabled,AssignedLicenses,LicenseAssignmentStates)
    $skus = @(Get-MgSubscribedSku -All)
    $inventory = [ordered]@{
        schemaVersion = 1
        tenantId = Get-Pseudonym 'tenant' $ctx.TenantId
        collectedAtUtc = $collected
        users = @(
            foreach ($u in $users) {
                if (-not $u.Id -or -not $u.UserPrincipalName -or $null -eq $u.AccountEnabled) { throw 'Benutzereigenschaften fehlen. Kein Teilinventar geschrieben.' }
                [ordered]@{
                    id = Get-Pseudonym 'user' $u.Id
                    userPrincipalName = [string]$u.UserPrincipalName
                    userType = [string]$u.UserType
                    accountEnabled = [bool]$u.AccountEnabled
                    assignedLicenses = @(
                        foreach ($a in $u.AssignedLicenses) {
                            [ordered]@{ skuId = [string]$a.SkuId; disabledPlans = @(foreach ($p in $a.DisabledPlans) { [string]$p }) }
                        }
                    )
                    licenseAssignmentStates = @(
                        foreach ($s in $u.LicenseAssignmentStates) {
                            $code = [string]$s.Error
                            if ($code -and $code -notmatch '^[A-Za-z][A-Za-z0-9_]{0,79}$') { $code = 'OtherError' }
                            [ordered]@{
                                skuId = [string]$s.SkuId
                                assignedByGroup = Get-Pseudonym 'group' $s.AssignedByGroup
                                disabledPlans = @(foreach ($p in $s.DisabledPlans) { [string]$p })
                                state = [string]$s.State
                                error = if ($code) { $code } else { $null }
                            }
                        }
                    )
                }
            }
        )
        subscribedSkus = @(
            foreach ($s in $skus) {
                [ordered]@{
                    skuId = [string]$s.SkuId
                    skuPartNumber = [string]$s.SkuPartNumber
                    servicePlans = @(
                        foreach ($p in $s.ServicePlans) {
                            [ordered]@{
                                servicePlanId = [string]$p.ServicePlanId
                                servicePlanName = [string]$p.ServicePlanName
                                provisioningStatus = [string]$p.ProvisioningStatus
                            }
                        }
                    )
                }
            }
        )
    }
    $json = ConvertTo-Json -InputObject $inventory -Depth 12
    $data = [Text.UTF8Encoding]::new($false).GetBytes($json + [Environment]::NewLine)
    $stream = [IO.FileStream]::new($full, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None)
    try { $stream.Write($data, 0, $data.Length) } finally { $stream.Dispose() }
} finally {
    $mac.Dispose()
    [Array]::Clear($key, 0, $key.Length)
}