DSCResources/MSFT_xRDServer/MSFT_xRDServer.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
Import-Module -Name "$PSScriptRoot\..\..\Modules\xRemoteDesktopSessionHostCommon.psm1"
if (!(Test-xRemoteDesktopSessionHostOsRequirement))
{
    throw "The minimum OS requirement was not met."
}
Import-Module RemoteDesktop
$localhost = [System.Net.Dns]::GetHostByName((hostname)).HostName

#######################################################################
# The Get-TargetResource cmdlet.
#######################################################################
function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $ConnectionBroker,

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

        [Parameter(Mandatory = $true)]
        [ValidateSet("RDS-Connection-Broker", "RDS-Virtualization", "RDS-RD-Server", "RDS-Web-Access", "RDS-Gateway", "RDS-Licensing")]
        [string]
        $Role,

        [Parameter()]
        [string]
        $GatewayExternalFqdn   # only for RDS-Gateway
    )

    $result = $null

    if (-not $ConnectionBroker)
    {
        $ConnectionBroker = $localhost
    }

    Write-Verbose "Getting list of servers of type '$Role' from '$ConnectionBroker'..."
    $servers = Get-RDServer -ConnectionBroker $ConnectionBroker -Role $Role -ea SilentlyContinue

    if ($servers)
    {
        Write-Verbose "Found $($servers.Count) '$Role' servers in the deployment, now looking for server named '$Server'..."

        if ($Server -in $servers.Server)
        {
            Write-Verbose "The server '$Server' is in the RD deployment."

            $result = @{
                ConnectionBroker    = $ConnectionBroker
                Server              = $Server
                Role                = $Role
                GatewayExternalFqdn = $null
            }

            if ($Role -eq 'RDS-Gateway')
            {
                Write-Verbose "the role is '$Role', querying RDS Gateway configuration..."

                $config = Get-RDDeploymentGatewayConfiguration -ConnectionBroker $ConnectionBroker

                if ($config)
                {
                    Write-Verbose "RDS Gateway configuration retrieved successfully..."
                    $result.GatewayExternalFqdn = $config.GatewayExternalFqdn
                    Write-Verbose ">> GatewayExternalFqdn: '$($result.GatewayExternalFqdn)'"
                }
            }
        }
        else
        {
            Write-Verbose "The server '$Server' is not in the deployment as '$Role' yet."
        }

    }
    else
    {
        Write-Verbose "No '$Role' servers found in the deployment on '$ConnectionBroker'."
        # or, possibly, Remote Desktop Deployment doesn't exist/Remote Desktop Management Service not running
    }

    $result
}


########################################################################
# The Set-TargetResource cmdlet.
########################################################################
function ValidateCustomModeParameters
{
    param
    (
        [Parameter()]
        [ValidateSet("RDS-Connection-Broker", "RDS-Virtualization", "RDS-RD-Server", "RDS-Web-Access", "RDS-Gateway", "RDS-Licensing")]
        [string]
        $Role,

        [Parameter()]
        [string]
        $GatewayExternalFqdn
    )

    Write-Verbose "validating parameters..."

    $customParams = @{
        GatewayExternalFqdn = $GatewayExternalFqdn
    }

    if ($Role -eq 'RDS-Gateway')
    {
        # ensure GatewayExternalFqdn was passed in, otherwise Add-RDServer will fail
        $emptyBoundParameters = $null
        $emptyBoundParameters = $customParams.getenumerator() | Where-Object { $_.value -eq [string]::Empty }

        if ($emptyBoundParameters)
        {
            $emptyBoundParameters | ForEach-Object { Write-Verbose ">> '$($_.Key)' parameter is empty" }

            Write-Warning "[PARAMETER VALIDATION FAILURE] i'm gonna throw, right now..."

            throw ("Requested server role 'RDS-Gateway', you must pass in the 'GatewayExternalFqdn' parameter.")
        }
    }
    else
    {
        # give warning about incorrect usage of the resource (do not fail)

        $parametersWithValues = $customParams.getenumerator() | Where-Object { $_.value }

        if ($parametersWithValues.count -gt 0)
        {
            $parametersWithValues | ForEach-Object { Write-Verbose ">> '$($_.Key)' was specified, the value is: '$($_.Value)'" }

            Write-Warning ("[WARNING]: Requested server role is '$Role', the following parameter can only be used with server role 'RDS-Gateway': " +
                "$($parametersWithValues.Key -join ', '). The parameter will be ignored in the call to Add-RDServer to avoid error!")
        }
    }
}


function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $ConnectionBroker,

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

        [Parameter(Mandatory = $true)]
        [ValidateSet("RDS-Connection-Broker", "RDS-Virtualization", "RDS-RD-Server", "RDS-Web-Access", "RDS-Gateway", "RDS-Licensing")]
        [string]
        $Role,

        [Parameter()]
        [string]
        $GatewayExternalFqdn   # only for RDS-Gateway
    )

    if (-not $ConnectionBroker)
    {
        $ConnectionBroker = $localhost
    }

    Write-Verbose "Adding server '$($Server.ToLower())' as $Role to the deployment on '$($ConnectionBroker.ToLower())'..."

    # validate parameters
    ValidateCustomModeParameters -Role $Role -GatewayExternalFqdn $GatewayExternalFqdn

    if ($Role -eq 'RDS-Gateway')
    {
        Write-Verbose ">> GatewayExternalFqdn: '$GatewayExternalFqdn'"
    }
    else
    {
        $PSBoundParameters.Remove("GatewayExternalFqdn")
    }


    Write-Verbose "calling Add-RDServer cmdlet..."
    #{
    if ($Role -eq 'RDS-Licensing' -or $Role -eq 'RDS-Gateway')
    {
        # workaround bug #3299246

        Add-RDServer @PSBoundParameters -erroraction silentlycontinue -errorvariable e

        if ($e.count -eq 0)
        {
            Write-Verbose "Add-RDServer completed without errors..."
            # continue
        }
        elseif ($e.count -eq 2 -and $e[0].FullyQualifiedErrorId -eq 'CommandNotFoundException')
        {
            Write-Verbose "Add-RDServer: trapped 2 errors, that's ok, continuing..."
            # ignore & continue
        }
        else
        {
            write-error "Add-RDServer threw $($e.count) errors."
        }
    }
    else
    {
        Add-RDServer @PSBoundParameters
    }
    #}
    Write-Verbose "Add-RDServer done."

}


#######################################################################
# The Test-TargetResource cmdlet.
#######################################################################
function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $ConnectionBroker,

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

        [Parameter(Mandatory = $true)]
        [ValidateSet("RDS-Connection-Broker", "RDS-Virtualization", "RDS-RD-Server", "RDS-Web-Access", "RDS-Gateway", "RDS-Licensing")]
        [string]
        $Role,

        [Parameter()]
        [string]
        $GatewayExternalFqdn   # only for RDS-Gateway
    )


    $target = Get-TargetResource @PSBoundParameters

    $result = $null -ne $target

    Write-Verbose "Test-TargetResource returning: $result"
    return $result
}


Export-ModuleMember -Function *-TargetResource