DSCResources/MSFT_xRDLicenseConfiguration/MSFT_xRDLicenseConfiguration.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
Import-Module -Name "$PSScriptRoot\..\..\Modules\xRemoteDesktopSessionHostCommon.psm1"
if (!(Test-xRemoteDesktopSessionHostOsRequirement))
{
    throw "The minimum OS requirement was not met."
}
Import-Module RemoteDesktop

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

        [Parameter()]
        [string[]]
        $LicenseServer,

        [Parameter(Mandatory = $true)]
        [ValidateSet("PerUser", "PerDevice", "NotConfigured")]
        [string]
        $LicenseMode
    )

    $result = $null

    Write-Verbose "Getting RD License server configuration from broker '$ConnectionBroker'..."

    $config = Get-RDLicenseConfiguration -ConnectionBroker $ConnectionBroker -ea SilentlyContinue

    if ($config)   # Microsoft.RemoteDesktopServices.Management.LicensingSetting
    {
        Write-Verbose "configuration retrieved successfully:"
    }
    else
    {
        Write-Verbose "Failed to retrieve RD License configuration from broker '$ConnectionBroker'."
        throw ("Failed to retrieve RD License configuration from broker '$ConnectionBroker'.")
    }
    $result =
    @{
        "ConnectionBroker" = $ConnectionBroker
        "LicenseServer"   = $config.LicenseServer
        "LicenseMode"      = $config.Mode.ToString()  # Microsoft.RemoteDesktopServices.Management.LicensingMode .ToString()
    }

    Write-Verbose ">> RD License mode: $($result.LicenseMode)"
    Write-Verbose ">> RD License servers: $($result.LicenseServer -join '; ')"

    $result
}


########################################################################
# The Set-TargetResource cmdlet.
########################################################################
function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $ConnectionBroker,

        [Parameter()]
        [string[]]
        $LicenseServer,

        [Parameter(Mandatory = $true)] # required parameter in Set-RDLicenseConfiguration
        [ValidateSet("PerUser", "PerDevice", "NotConfigured")]
        [string]
        $LicenseMode
    )

    Write-Verbose "Starting RD License server configuration..."
    Write-Verbose ">> RD Connection Broker: $($ConnectionBroker.ToLower())"

    if ($LicenseServer)
    {
        Write-Verbose ">> RD License servers: $($LicenseServer -join '; ')"

        Write-Verbose "calling Set-RDLicenseConfiguration cmdlet..."
        Set-RDLicenseConfiguration -ConnectionBroker $ConnectionBroker -LicenseServer $LicenseServer -Mode $LicenseMode -Force
    }
    else
    {
        Write-Verbose "calling Set-RDLicenseConfiguration cmdlet..."
        Set-RDLicenseConfiguration -ConnectionBroker $ConnectionBroker -Mode $LicenseMode -Force
    }

    Write-Verbose "Set-RDLicenseConfiguration done."
}


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

        [Parameter()]
        [string[]]
        $LicenseServer,

        [Parameter(Mandatory = $true)]
        [ValidateSet("PerUser", "PerDevice", "NotConfigured")]
        [string] $LicenseMode
    )

    $config = Get-TargetResource @PSBoundParameters

    if ($config)
    {
        Write-Verbose "Verifying RD Licensing mode: $($config.LicenseMode -eq $LicenseMode)"

        Write-Verbose "Verifying RD license servers..."
        $noChange = $true
        if ($LicenseServer)
        {
            foreach ($server in $config.LicenseServer)
            {
                if ($LicenseServer -notcontains $server)
                {
                    $noChange = $false
                    Write-Verbose "License Server '$server' in the current configuration will be removed."
                }
            }
            if ($LicenseServer.Count -ne $config.LicenseServer.Count)
            {
                $noChange = $false
            }
        }


        $result = ($config.LicenseMode -eq $LicenseMode) -and $noChange
    }
    else
    {
        Write-Verbose "Failed to retrieve RD License server configuration from broker '$ConnectionBroker'."
        $result = $false
    }

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


Export-ModuleMember -Function *-TargetResource