Public/Export-KritTcmSnapshotToFile.ps1
|
function Export-KritTcmSnapshotToFile { <# .SYNOPSIS Download a completed TCM snapshot extraction to disk. #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory, ValueFromPipelineByPropertyName)][Alias('Id')][string]$JobId, [Parameter(Mandatory)][string]$OutPath, [switch]$Force ) process { $job = Get-KritTcmSnapshot -JobId $JobId -Select 'id,displayName,status,resourceLocation,completedDateTime,errorDetails,resources' if (-not $job.RawResponse.resourceLocation) { throw "Snapshot job $JobId does not expose resourceLocation. Current status: $($job.Status)." } if ((Test-Path -LiteralPath $OutPath) -and -not $Force) { throw "Output path already exists: $OutPath. Use -Force to overwrite." } if ($PSCmdlet.ShouldProcess($OutPath, "Download TCM snapshot job $JobId")) { $content = Invoke-MgGraphRequest -Method GET -Uri $job.RawResponse.resourceLocation -ErrorAction Stop New-Item -ItemType Directory -Path (Split-Path -Parent $OutPath) -Force | Out-Null $content | ConvertTo-Json -Depth 64 | Set-Content -LiteralPath $OutPath -Encoding UTF8 [pscustomobject]@{ Action = 'Export-KritTcmSnapshotToFile' JobId = $JobId Status = $job.Status OutPath = (Resolve-Path -LiteralPath $OutPath).Path ResourceLocation = $job.RawResponse.resourceLocation } } } } |