Public/Get-BridgePreviousStatus.ps1
|
<# .SYNOPSIS Reads the previously saved bridge status from a JSON file. .DESCRIPTION Reads the state file generated by Export-BridgeStatusJson and returns the records as PSCustomObjects. Filters out any records that do not match the expected schema. .PARAMETER InputFile The path to the JSON file containing the previous status. .PARAMETER JsonDepth The maximum depth to parse the JSON file. Defaults to 10. .PARAMETER Configuration The configuration object containing logging settings. .EXAMPLE Get-BridgePreviousStatus -InputFile 'C:\temp\status.json' -Configuration $Config .OUTPUTS [PSCustomObject[]] #> function Get-BridgePreviousStatus { [CmdletBinding()] [OutputType([object[]])] param ( [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$InputFile, [Parameter()][ValidateRange(1, 20)][int]$JsonDepth = 10, [Parameter(Mandatory)][ValidateNotNull()][PSCustomObject]$Configuration ) begin { } process { if (-not (Test-Path $InputFile)) { $writeBridgeLogSplat = @{ Stage = $Configuration.LoggingConfig.ErrorStage Message = "Το αρχείο $($InputFile) δεν βρέθηκε – επιστρέφεται κενό array." Level = $Configuration.LoggingConfig.WarningLevel } Write-BridgeLog @writeBridgeLogSplat -Configuration $Configuration return @() } try { $getContentSplat = @{ Path = $InputFile Raw = $true Encoding = 'utf8BOM' } $convertFromJsonSplat = @{ Depth = $JsonDepth } $results = Get-Content @getContentSplat | ConvertFrom-Json @convertFromJsonSplat # Φιλτράρισμα άκυρων εγγραφών (π.χ. αν αποθηκεύτηκε BridgeResult αντί για Array) $validResults = @($results) | Where-Object { $null -ne $_ -and $_.psobject.properties.match('GefyraName').Count -gt 0 } if ($validResults.Count -eq 0) { return @() } # Backfill ImageHash for state files saved before this property existed foreach ($item in $validResults) { if (-not ($item.PSObject.Properties.Name -contains 'ImageHash')) { $item | Add-Member -MemberType NoteProperty -Name 'ImageHash' -Value $null } } return $validResults } catch { $writeBridgeLogSplat = @{ Stage = $Configuration.LoggingConfig.ErrorStage Message = "Σφάλμα κατά την ανάλυση JSON: $($_.Exception.Message)" Level = $Configuration.LoggingConfig.WarningLevel } Write-BridgeLog @writeBridgeLogSplat -Configuration $Configuration $PSCmdlet.ThrowTerminatingError( [System.Management.Automation.ErrorRecord]::new( ([System.Exception]::new("Σφάλμα κατά την ανάλυση JSON: $($_.Exception.Message)", $_.Exception)), 'BridgeJsonParseError', [System.Management.Automation.ErrorCategory]::InvalidData, $InputFile ) ) } } } |