Modules/businessdev.ALbuild.RuntimePackages/Private/Get-BcRuntimeProperty.ps1
|
function Get-BcRuntimeProperty { <# .SYNOPSIS Reads an optional property from a catalogue entry without tripping Set-StrictMode. .DESCRIPTION Catalogue entries come from a JSON file, a pipeline parameter or a test fixture, so optional fields (Country, Projects, DependsOn, ...) are frequently just absent. Under 'Set-StrictMode -Version Latest' - which every ALbuild module sets - reading a property that does not exist is a terminating error, not $null. Making every optional field mandatory to dodge that would push the problem onto every caller and every fixture; reading through PSObject.Properties returns $null for a missing member instead of throwing, which is the behaviour the call sites actually want. .PARAMETER InputObject The object to read from. .PARAMETER Name The property name. .PARAMETER Default Value to return when the property is absent or null. Defaults to $null. .OUTPUTS The property value, or -Default. #> [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateNotNull()] [object] $InputObject, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Name, [object] $Default = $null ) if ($InputObject -is [System.Collections.IDictionary]) { if ($InputObject.Contains($Name) -and $null -ne $InputObject[$Name]) { return $InputObject[$Name] } return $Default } $property = $InputObject.PSObject.Properties[$Name] if ($null -eq $property -or $null -eq $property.Value) { return $Default } return $property.Value } |