Private/Parameter.ps1

# A Kind's parameters arrive from a Check Definition, which is JSON. Windows PowerShell 5.1
# has no ConvertFrom-Json -AsHashtable, so Definitions are normalised to hashtables on the
# way in and read through these helpers, which supply the Kind's default when a Definition
# is silent about a parameter.

function Get-Parameter {
    [CmdletBinding()]
    param(
        [AllowNull()][hashtable]$Parameters,
        [Parameter(Mandatory)][string]$Name,
        $Default
    )
    if ($Parameters -and $Parameters.ContainsKey($Name) -and $null -ne $Parameters[$Name]) {
        return $Parameters[$Name]
    }
    $Default
}

function ConvertTo-ParameterHashtable {
    <#
    .SYNOPSIS
        Turns the object ConvertFrom-Json produced into a hashtable, recursively.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param([AllowNull()]$InputObject)

    if ($null -eq $InputObject) { return @{} }
    if ($InputObject -is [hashtable]) { return $InputObject }

    $result = @{}
    foreach ($property in $InputObject.PSObject.Properties) {
        $value = $property.Value
        if ($value -is [psobject] -and $value.PSObject.Properties.Name -and
            $value -isnot [string] -and $value -isnot [ValueType] -and $value -isnot [array]) {
            $value = ConvertTo-ParameterHashtable $value
        }
        $result[$property.Name] = $value
    }
    $result
}