<# Flow-Rettungsakte - lokaler Leseexport fuer ausgewaehlte Umgebungen. Vorab separat mit Microsoft.PowerApps.Administration.PowerShell per Add-PowerAppsAccount anmelden. Die Get-Admin*-Cmdlets koennen eine Adminrolle voraussetzen; das Skript macht breite Rollen nicht zu Nur-Lese-Rollen. Optional vorab Connect-MgGraph -Scopes User.Read.All mit den Modulen Microsoft.Graph.Authentication und Microsoft.Graph.Users; dann -Graph nutzen. Aufruf: .\flow-rescue.ps1 -EnvironmentName 'Default-' Nur ein Mandant je Aufruf. Keine Definitionen, Payloads oder Geheimnisse im Export. Angezeigte Flow-Namen bleiben lesbar. Pseudonyme sind nur innerhalb dieses Exports stabil; manuelle CSVs aus der Browser-Vorlage ableiten. Nicht fixture-gepruefte Eigenschaften bleiben unbekannt. Keine Verbindung aus Connector-Gleichheit, Ersteller oder Rolle ableiten. Bei API-Abweichung manuelles Inventar bzw. manuelle Nachweise verwenden. #> [CmdletBinding()] param( [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string[]]$EnvironmentName, [switch]$Graph ) $ErrorActionPreference = 'Stop' $inventoryCmd = Get-Command Get-AdminFlow -ErrorAction Stop $roleCmd = Get-Command Get-AdminFlowOwnerRole -ErrorAction SilentlyContinue $graphCmd = if ($Graph) { Get-Command Get-MgUser -ErrorAction SilentlyContinue } function NowUtc { [DateTime]::UtcNow.ToString('o',[Globalization.CultureInfo]::InvariantCulture) } function VersionOf($cmd) { if ($cmd -and $cmd.Module -and $cmd.Module.Version) { [string]$cmd.Module.Version } else { '' } } function LocalId([string]$Kind,[string]$Value) { $msg = [Text.Encoding]::UTF8.GetBytes($Kind + ':' + $Value.Trim().ToLowerInvariant()) $hex = [BitConverter]::ToString($mac.ComputeHash($msg)).Replace('-','').ToLowerInvariant() return $Kind + '-' + $hex } function Source([string]$Name,$Cmd,[string]$Status) { [ordered]@{source=$Name;module_version=(VersionOf $Cmd);status=$Status;observed_utc=(NowUtc)} } function Coverage([string]$Env,[string]$Flow,[string]$Group,[string]$Reason) { [ordered]@{environment_id=$Env;flow_id=$Flow;field_group=$Group;status='unknown';reason_code=$Reason} } $seed = New-Object byte[] 32 $rng = [Security.Cryptography.RandomNumberGenerator]::Create() $rng.GetBytes($seed) $rng.Dispose() $mac = New-Object Security.Cryptography.HMACSHA256 $mac.Key = $seed [Array]::Clear($seed,0,$seed.Length) $flows = [Collections.Generic.List[object]]::new() $owners = [Collections.Generic.List[object]]::new() $coverage = [Collections.Generic.List[object]]::new() $sources = [Collections.Generic.List[object]]::new() $accounts = @{} $flowKeys = @{} $roleOk = 0; $roleError = 0; $rolePartial = 0; $graphOk = 0; $graphError = 0; $inventoryError = 0 $start = NowUtc try { foreach ($inputEnv in ($EnvironmentName | ForEach-Object { $_.Trim() } | Sort-Object -Unique)) { if ([string]::IsNullOrWhiteSpace($inputEnv)) { throw 'Leere Umgebungskennung.' } $eid = LocalId 'env' $inputEnv try { $raw = @(Get-AdminFlow -EnvironmentName $inputEnv -ErrorAction Stop -WarningAction SilentlyContinue) } catch { $inventoryError++; continue } foreach ($f in $raw) { $fid = [string]$f.FlowName $display = [string]$f.DisplayName if ([string]::IsNullOrWhiteSpace($fid) -or [string]::IsNullOrWhiteSpace($display)) { $inventoryError++; continue } $flowId = LocalId 'flow' ($inputEnv + '/' + $fid) if ($flowKeys.ContainsKey($flowId)) { throw 'Doppelter Flow-Schluessel in der Cmdlet-Antwort; kein Export geschrieben.' } $flowKeys[$flowId] = $true $flows.Add([ordered]@{environment_id=$eid;flow_id=$flowId;display_name=$display;flow_state='unknown';last_modified_utc='';solution_aware='unknown'}) $coverage.Add((Coverage $eid $flowId 'inventory_details' 'module_fields_not_verified')) $coverage.Add((Coverage $eid $flowId 'connections' 'reference_metadata_not_verified')) $coverage.Add((Coverage $eid $flowId 'primary_owner' 'manual_confirmation_required')) if (-not $roleCmd) { $coverage.Add((Coverage $eid $flowId 'roles' 'cmdlet_unavailable')); continue } try { $rr = @(Get-AdminFlowOwnerRole -EnvironmentName $inputEnv -FlowName $fid -ErrorAction Stop -WarningAction SilentlyContinue); $roleOk++ } catch { $roleError++; $coverage.Add((Coverage $eid $flowId 'roles' 'read_failed')); continue } $roles = @{} $localPartial = $false foreach ($r in $rr) { $pid = [string]$r.PrincipalObjectId if ($pid -notmatch '^[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$') { $localPartial = $true; continue } $type = switch -Regex ([string]$r.PrincipalType) { '^user$' { 'user'; break } '^(group|serviceprincipal|application)$' { 'other'; break } default { 'unknown' } } $rn = [string]$r.RoleName if ($rn -notmatch '^[a-zA-Z][a-zA-Z0-9_-]{0,63}$') { $rn = '' } if (-not $roles.ContainsKey($pid)) { $roles[$pid] = @{ Type = $type; Names = @(); Ambiguous = $false } } $entry = $roles[$pid] if ($entry.Type -ne $type) { $entry.Ambiguous = $true } if ($rn -and $entry.Names -notcontains $rn) { $entry.Names += $rn } } if ($localPartial -or $roles.Count -eq 0) { $rolePartial++; $coverage.Add((Coverage $eid $flowId 'roles' 'empty_or_unmapped_roles')) } foreach ($pid in ($roles.Keys | Sort-Object)) { $r = $roles[$pid] $type = if ($r.Ambiguous) { 'unknown' } else { $r.Type } $state = 'unknown'; $observed = NowUtc if ($Graph -and $graphCmd -and $type -eq 'user') { if (-not $accounts.ContainsKey($pid)) { $s = 'unknown' try { $u = Get-MgUser -UserId $pid -Property @('Id','AccountEnabled','UserType') -ErrorAction Stop -WarningAction SilentlyContinue if ($u.Id -eq $pid -and $u.AccountEnabled -is [bool]) { if ($u.AccountEnabled) { $s = 'enabled' } else { $s = 'disabled' }; $graphOk++ } else { $graphError++ } } catch { $graphError++ } $accounts[$pid] = @{ State = $s; Observed = (NowUtc) } } $state = $accounts[$pid].State; $observed = $accounts[$pid].Observed } $owners.Add([ordered]@{environment_id=$eid;flow_id=$flowId;owner_ref=(LocalId 'principal' $pid);principal_type=$type;source_role=(($r.Names | Sort-Object) -join '|');owner_evidence='role_only';account_state=$state;departure_confirmed='unknown';primary_state='unknown';observed_utc=$observed}) } } } if ($flows.Count -eq 0) { throw 'Kein lesbares Cloud-Flow-Inventar. Umgebungen, Berechtigungen und Modulstand pruefen oder CSV-Vorlage verwenden. Keine Datei geschrieben.' } $invStatus = if ($inventoryError) { 'partial' } else { 'ok' } $roleStatus = if (-not $roleCmd) { 'unavailable' } elseif ($roleError -and -not $roleOk) { 'error' } elseif ($roleError -or $rolePartial) { 'partial' } else { 'ok' } $graphStatus = if (-not $Graph) { 'not_requested' } elseif (-not $graphCmd) { 'unavailable' } elseif ($graphError -and -not $graphOk) { 'error' } elseif ($graphError) { 'partial' } elseif ($graphOk) { 'ok' } else { 'no_known_users' } $sources.Add((Source 'Get-AdminFlow' $inventoryCmd $invStatus)) $sources.Add((Source 'Get-AdminFlowOwnerRole' $roleCmd $roleStatus)) $sources.Add((Source 'Get-MgUser' $graphCmd $graphStatus)) $sources.Add((Source 'Get-AdminPowerAppConnection' $null 'not_requested')) $doc = [ordered]@{schema_version=1;snapshot_utc=$start;sources=$sources.ToArray();flows=$flows.ToArray();owners=$owners.ToArray();connections=@();coverage=$coverage.ToArray()} $here = Get-Location if ($here.Provider.Name -ne 'FileSystem') { throw 'Bitte aus einem lokalen Dateisystemordner ausfuehren.' } $out = [IO.Path]::Combine($here.ProviderPath,'flow-rescue.v1.json') $json = ConvertTo-Json -InputObject $doc -Depth 8 $bytes = [Text.UTF8Encoding]::new($false).GetBytes($json) if ($bytes.Length -gt 10MB) { throw 'Export ueberschreitet das Browserlimit. Weniger Umgebungen auswaehlen.' } $stream = [IO.File]::Open($out,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None) try { $stream.Write($bytes,0,$bytes.Length) } finally { $stream.Dispose() } Write-Output 'Lokaler Export erstellt: flow-rescue.v1.json' } finally { $mac.Dispose() }