DSCResources/ResourceSetHelper.psm1

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Set-StrictMode -version Latest

<#
    .SYNOPSIS
    Builds a string with all common parameters shared across all resource nodes.
#>

function New-ResourceCommonParameterString
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $KeyParameterName,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $CommonParameterNames,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Collections.Hashtable]
        $Parameters
    )

    [System.Text.StringBuilder] $stringBuilder = New-Object System.Text.StringBuilder

    # Build optional parameters for invoking the inbuilt resource
    foreach ($parameterName in $Parameters.Keys) 
    {
        if ($parameterName -ne $KeyParameterName -and $parameterName -in $CommonParameterNames)
        {
            $parameterValue = $Parameters[$parameterName]
            if ($null -ne $parameterValue)
            {
                if ($parameterValue -is [System.String])
                {
                    $stringBuilder.AppendFormat('{0} = "{1}"', $parameterName, $parameterValue) | Out-Null
                    $stringBuilder.AppendLine() | Out-Null
                }
                else
                {
                    $stringBuilder.Append($parameterName + ' = $' + $parameterName) | Out-Null
                    $stringBuilder.AppendLine() | Out-Null
                }
            }
        }
    }

    return $stringBuilder.ToString()
}

<#
    .SYNOPSIS
    Builds a string with all resource nodes.
 
    .DESCRIPTION
    Builds a string with all resource nodes based on the key parameter along with the other optional parameters provided in the composite configuration.
    For example, while installing multiple features using WindowsFeature resource, output of this method would be:
        $KeyParam = @("Telnet-client","web-server")
        WindowsFeature Resource0
        {
        Name = "Telnet-client" #Name provided in the composite configuration
        Ensure = "Present"
        IncludeAllSubFeature = True
        }
 
        WindowsFeature Resource1
        {
        Name = "Web-Server" #Name provided in the composite configuration
        Ensure = "Present"
        IncludeAllSubFeature = True
        }
#>

function New-ResourceString
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $KeyParameterValues, 
    
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $KeyParameterName,
    
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $CommonParameters,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $ResourceName,

        [ValidateNotNullOrEmpty()]
        [string]
        $ModuleName = "xPSDesiredStateConfiguration",

        [ValidateNotNullOrEmpty()]
        [string]
        $DefaultParameters
    )

    [System.Text.StringBuilder] $stringBuilder = New-Object System.Text.StringBuilder

    $stringBuilder.AppendFormat('Import-DscResource -Name {0} -ModuleName {1}', $ResourceName, $ModuleName) | Out-Null
    $stringBuilder.AppendLine() | Out-Null

    # Add the resource nodes and their common parameters
    [int] $resourceCount = 0
    foreach ($keyParameterValue in $KeyParameterValues)
    {
        $stringBuilder.AppendFormat('{0} Resource{1}', $ResourceName, $resourceCount) | Out-Null
        $stringBuilder.AppendLine() | Out-Null
        $stringBuilder.AppendLine('{') | Out-Null
        $stringBuilder.AppendFormat($KeyParameterName + ' = "{0}"', $keyParameterValue) | Out-Null
        $stringBuilder.AppendLine() | Out-Null
        $stringBuilder.AppendLine($CommonParameters) | Out-Null
        
        if ($DefaultParameters)
        {
            $stringBuilder.AppendLine($DefaultParameters) | Out-Null
        }

        $stringBuilder.AppendLine('}') | Out-Null
        $resourceCount++
    }

    return $stringBuilder.ToString()
}

Export-ModuleMember -Function `
    New-ResourceCommonParameterString, `
    New-ResourceString