#Requires -Version 7.0 #Requires -Modules Microsoft.Graph.Authentication, Microsoft.Graph.Identity.DirectoryManagement <# LUNA Bestellfreigabe – optionaler lokaler Mandantenabgleich. Aufruf: .\tenant-referenz.ps1 -ExpectedTenantId Verwendet delegierten Lesezugriff Organization.Read.All direkt im angegebenen Tenant, ohne LUNA-Entra-App. Bei fehlendem genehmigtem Zugriff den Export überspringen. Schreibt genau eine lokale JSON-Datei. Keine Benutzer- oder Lizenzdaten, keine Cloud-Änderungen. Für den exakten Abgleich enthält die Datei die echte Organisations-GUID, keine Namen oder E-Mail-Adressen. Die GUID ist nicht anonym. Das Verzeichnisland ist nur ein Hinweis und kein Nachweis für Kundenmarkt, CSP-Berechtigung oder MCA-Annahme. #> [CmdletBinding()] param( [Parameter(Mandatory)] [guid]$ExpectedTenantId, [ValidateNotNullOrEmpty()] [string]$LocalOutputPath = '.\tenant_referenz.json' ) $ErrorActionPreference = 'Stop' if ($ExpectedTenantId -eq [guid]::Empty) { throw 'Die erwartete Mandanten-ID darf keine Null-GUID sein.' } $provider = $null $drive = $null $path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($LocalOutputPath, [ref]$provider, [ref]$drive) if ($provider.Name -ne 'FileSystem' -or [System.IO.Path]::GetExtension($path) -ine '.json' -or $path.StartsWith('\\')) { throw 'Bitte einen lokalen Dateipfad mit Endung .json angeben.' } Connect-MgGraph -TenantId ($ExpectedTenantId.ToString('D')) -Scopes 'Organization.Read.All' -ContextScope Process -NoWelcome -ErrorAction Stop $context = Get-MgContext -ErrorAction Stop $sessionId = [guid]::Empty if ($null -eq $context -or -not [guid]::TryParse([string]$context.TenantId, [ref]$sessionId) -or $sessionId -ne $ExpectedTenantId -or $context.AuthType -ne 'Delegated') { throw 'Die delegierte Microsoft-Graph-Sitzung gehört nicht eindeutig zum erwarteten Mandanten. Keine Datei geschrieben.' } $orgs = @(Get-MgOrganization -Property 'id,countryLetterCode' -All -ErrorAction Stop) if ($orgs.Count -ne 1) { throw 'Genau ein Organisationsdatensatz erwartet. Keine Datei geschrieben.' } $orgId = [guid]::Empty if (-not [guid]::TryParse([string]$orgs[0].Id, [ref]$orgId) -or $orgId -ne $ExpectedTenantId) { throw 'Die Organisations-ID entspricht nicht dem erwarteten Mandanten. Keine Datei geschrieben.' } $country = ([string]$orgs[0].CountryLetterCode).Trim() if ($country.Length -eq 0) { $country = $null } $record = [ordered]@{ schemaVersion = 1 exportedAtUtc = [datetime]::UtcNow.ToString('o', [Globalization.CultureInfo]::InvariantCulture) tenantId = $orgId.ToString('D') directoryCountryLetterCode = $country } $record | ConvertTo-Json -Depth 2 | Set-Content -LiteralPath $path -Encoding utf8 -ErrorAction Stop