Private/Compat.ps1
|
# Runs on Windows PowerShell 5.1 and PowerShell 7.2+. Keep 7-only syntax (??, ?., ternary, -Parallel, ConvertFrom-Json -Depth/-AsHashtable) out of the module. function ConvertFrom-STMJson { param([Parameter(ValueFromPipeline)][string]$Json) process { if ([string]::IsNullOrWhiteSpace($Json)) { return $null } if ($PSVersionTable.PSVersion.Major -ge 6) { return ($Json | ConvertFrom-Json -Depth 100) } return ($Json | ConvertFrom-Json) } } function ConvertTo-STMFastJson { # Fast serializer for large payloads (inventory rows). Values must be plain strings, numbers, booleans, arrays or hashtables. param($Value) if ($PSVersionTable.PSVersion.Major -ge 6) { return [System.Text.Json.JsonSerializer]::Serialize($Value, [object]) } if (-not $script:STMJss) { Add-Type -AssemblyName System.Web.Extensions $script:STMJss = New-Object System.Web.Script.Serialization.JavaScriptSerializer $script:STMJss.MaxJsonLength = [int]::MaxValue $script:STMJss.RecursionLimit = 100 } return $script:STMJss.Serialize($Value) } function Test-STMIsWindows { if ($PSVersionTable.PSEdition -eq 'Desktop') { return $true } return [bool]$IsWindows } |