mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-02-17 07:56:24 +00:00
33 lines
997 B
PowerShell
33 lines
997 B
PowerShell
|
|
# Loads a JSON file from the specified path and returns the parsed object
|
||
|
|
# Returns $null if the file doesn't exist or if parsing fails
|
||
|
|
function LoadJsonFile {
|
||
|
|
param (
|
||
|
|
[string]$filePath,
|
||
|
|
[string]$expectedVersion = $null,
|
||
|
|
[switch]$optionalFile
|
||
|
|
)
|
||
|
|
|
||
|
|
if (-not (Test-Path $filePath)) {
|
||
|
|
if (-not $optionalFile) {
|
||
|
|
Write-Error "File not found: $filePath"
|
||
|
|
}
|
||
|
|
return $null
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$jsonContent = Get-Content -Path $filePath -Raw | ConvertFrom-Json
|
||
|
|
|
||
|
|
# Validate version if specified
|
||
|
|
if ($expectedVersion -and $jsonContent.Version -and $jsonContent.Version -ne $expectedVersion) {
|
||
|
|
Write-Error "$(Split-Path $filePath -Leaf) version mismatch (expected $expectedVersion, found $($jsonContent.Version))"
|
||
|
|
return $null
|
||
|
|
}
|
||
|
|
|
||
|
|
return $jsonContent
|
||
|
|
}
|
||
|
|
catch {
|
||
|
|
Write-Error "Failed to parse JSON file: $filePath"
|
||
|
|
return $null
|
||
|
|
}
|
||
|
|
}
|