ScriptsToProcess/Resolve-NodeProperty.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function Global:Resolve-NodeProperty {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory,
            Position = 0
        )]
        $PropertyPath,

        [Parameter(
            Position = 1
        )]
        [AllowNull()]
        $DefaultValue,

        [Parameter(
            Position = 3
        )]
        $Node = $ExecutionContext.InvokeCommand.InvokeScript('$Node'),

        [Alias('DatumStructure')]
        $DatumTree = $ExecutionContext.InvokeCommand.InvokeScript('$ConfigurationData.Datum'),

        [Alias('SearchBehavior')]
        [AllowNull()]
        $options,

        [string[]]
        $SearchPaths,

        [Parameter(
            Position = 5
        )]
        [allowNull()]
        [int]
        $MaxDepth
    )

    if ($Node -is [string] -and ($ConfigData = $ExecutionContext.InvokeCommand.InvokeScript('$ConfigurationData'))) {
        $Node = $ConfigData.AllNodes.Where{$_.Name -eq $Node -or $_.NodeName -eq $Node}
    }

    # Null result should return an exception, unless defined as Default value
    $NullAllowed = $false

    $ResolveDatumParams = ([hashtable]$PSBoundParameters).Clone()
    foreach ($removeKey in $PSBoundParameters.keys.where{$_ -in @('DefaultValue','Node')}) {
        $ResolveDatumParams.remove($removeKey)
    }

    # Translate the DSC specific Node into the 'Node' variable and Node name used by Resolve-Datum
    if($Node) {
        $ResolveDatumParams.Add('Variable',$Node)
        $ResolveDatumParams.Add('VariableName','Node')
    }

    # Starting DSC Behaviour: Resolve-Datum || $DefaultValue || $null if specified as default || throw
    if($result = Resolve-Datum @ResolveDatumParams) {
        Write-Verbose "`tResult found for $PropertyPath"
    }
    elseif($DefaultValue) {
        $result = $DefaultValue
        Write-Debug "`t`tDefault Found"
    }
    elseif($PSboundParameters.containsKey('DefaultValue') -and $null -eq $DefaultValue) {
        $result = $null
        $NullAllowed = $true
        Write-Debug "`t`tDefault NULL found and allowed."
    }
    else {
        #This is when the Lookup is initiated from a Composite Resource, for itself

        if(-not ($here = $MyInvocation.PSScriptRoot)) {
            $here = $Pwd.Path
        }
        Write-Debug "`t`tAttempting to load datum from $($here)."

        $ResourceConfigDataPath = Join-Path $here 'ConfigData' -Resolve -ErrorAction SilentlyContinue

        if($ResourceConfigDataPath) {
            $DatumDefinitionFile = Join-Path $ResourceConfigDataPath 'Datum.*' -Resolve -ErrorAction SilentlyContinue
            if($DatumDefinitionFile) {
                Write-Debug "Resource Datum File Path: $DatumDefinitionFile"
                $ResourceDatum = New-DatumStructure -DefinitionFile $DatumDefinitionFile
            }
            else {
                #Loading Default Datum structure
                Write-Debug "Loading data store from $($ResourceConfigDataPath)."
                $ResourceDatum = New-DatumStructure -DatumHierarchyDefinition @{
                    Path = $ResourceConfigDataPath
                }
            }
            $ResolveDatumParams.remove('DatumTree')

            $result = Resolve-Datum @ResolveDatumParams -DatumTree $ResourceDatum
        }
        else {
            Write-Warning "`tNo Datum store found for DSC Resource"
        }
    }

    if($null -ne $result -or $NullAllowed) {
        ,$result
    }
    else {
        throw "The lookup of path '$PropertyPath' for node '$($node.Name)' returned a Null value, but Null is not specified as Default. This is not allowed."
    }
}
Set-Alias -Name Lookup -Value Resolve-NodeProperty -scope Global
Set-Alias -Name Resolve-DscProperty -Value Resolve-NodeProperty -scope Global