<# Lizenz-Kompass für Microsoft 365 Copilot – lokales Leseinventar (PowerShell 7). Voraussetzung: eigene delegierte Graph-PowerShell-Sitzung in derselben Shell, Module Microsoft.Graph.Users und Microsoft.Graph.Identity.DirectoryManagement. Die Kundenadministration prüft Rolle und delegierte Leserechte User.Read.All und LicenseAssignment.Read.All. Kein Modul-Download, keine Verbindungseröffnung, keine Mandantenänderung und kein Upload. Nur die beiden Get-Mg*-Abfragen lesen Mandantendaten. Geschrieben wird genau eine lokale JSON-Datei. Das Feld userPrincipalName enthält ausschließlich ein dateispezifisches HMAC-Pseudonym. Der zufällige Schlüssel bleibt im Prozess und wird nicht ausgegeben. Kein Join mit UPN-CSV möglich. Aufruf: .\lizenzkompass.ps1 #> #Requires -Version 7.0 $ErrorActionPreference = 'Stop' $Destination = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.\lizenzkompass-inventar.json') if ([IO.File]::Exists($Destination)) { throw 'Die Ausgabedatei existiert bereits. Bitte zuerst lokal umbenennen oder entfernen.' } $Users = @(Get-MgUser -All -Property UserPrincipalName,AccountEnabled,AssignedLicenses) $Skus = @(Get-MgSubscribedSku -All -Property SkuId,SkuPartNumber) $Collected = [DateTime]::UtcNow.ToString('o', [Globalization.CultureInfo]::InvariantCulture) $Key = [Security.Cryptography.RandomNumberGenerator]::GetBytes(32) $Mac = [Security.Cryptography.HMACSHA256]::new($Key) try { $UserRows = @(foreach ($User in $Users) { $Upn = ([string]$User.UserPrincipalName).Trim().ToLowerInvariant() if (-not $Upn) { throw 'Mindestens ein Benutzer hat keinen lesbaren UPN. Inventar nicht geschrieben.' } $Hash = $Mac.ComputeHash([Text.Encoding]::UTF8.GetBytes($Upn)) $Pseudonym = 'lk-' + [Convert]::ToHexString($Hash).ToLowerInvariant() $Assigned = @(foreach ($Assignment in $User.AssignedLicenses) { if ($null -eq $Assignment -or -not $Assignment.SkuId) { throw 'Eine zugewiesene SKU-ID fehlt.' } ([guid]$Assignment.SkuId).ToString('D') }) [ordered]@{ userPrincipalName = $Pseudonym; accountEnabled = $User.AccountEnabled; assignedSkuIds = $Assigned } }) $SkuRows = @(foreach ($Sku in $Skus) { if (-not $Sku.SkuId -or -not ([string]$Sku.SkuPartNumber).Trim()) { throw 'Eine abonnierte SKU ist unvollständig.' } [ordered]@{ skuId = ([guid]$Sku.SkuId).ToString('D'); skuPartNumber = [string]$Sku.SkuPartNumber } }) $Result = [ordered]@{ schemaVersion = 1; collectedAtUtc = $Collected; idMode = 'file-hmac-sha256'; users = $UserRows; skus = $SkuRows } $Json = ConvertTo-Json -InputObject $Result -Depth 8 $Bytes = [Text.UTF8Encoding]::new($false).GetBytes($Json) $Stream = [IO.File]::Open($Destination, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None) try { $Stream.Write($Bytes, 0, $Bytes.Length) } finally { $Stream.Dispose() } } finally { $Mac.Dispose() [Array]::Clear($Key, 0, $Key.Length) }