DSCResources/MSFT_xRDSessionCollectionConfiguration/MSFT_xRDSessionCollectionConfiguration.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
Import-Module -Name "$PSScriptRoot\..\..\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(Mandatory)]
        [ValidateLength(1,15)]
        [string] $CollectionName,
        [uint32] $ActiveSessionLimitMin,
        [boolean] $AuthenticateUsingNLA,
        [boolean] $AutomaticReconnectionEnabled,
        [string] $BrokenConnectionAction,
        [string] $ClientDeviceRedirectionOptions,
        [boolean] $ClientPrinterAsDefault,
        [boolean] $ClientPrinterRedirected,
        [string] $CollectionDescription,
        [string] $ConnectionBroker,
        [string] $CustomRdpProperty,
        [uint32] $DisconnectedSessionLimitMin,
        [string] $EncryptionLevel,
        [uint32] $IdleSessionLimitMin,
        [uint32] $MaxRedirectedMonitors,
        [boolean] $RDEasyPrintDriverEnabled,
        [string] $SecurityLayer,
        [boolean] $TemporaryFoldersDeletedOnExit,
        [string] $UserGroup
    )
        Write-Verbose "Getting currently configured RDSH Collection properties"
        $collectionName = Get-RDSessionCollection | % {Get-RDSessionHost $_.CollectionName} | ? {$_.SessionHost -ieq $localhost} | % {$_.CollectionName}

        $collectionGeneral = Get-RDSessionCollectionConfiguration -CollectionName $CollectionName
        $collectionClient = Get-RDSessionCollectionConfiguration -CollectionName $CollectionName -Client
        $collectionConnection = Get-RDSessionCollectionConfiguration -CollectionName $CollectionName -Connection
        $collectionSecurity = Get-RDSessionCollectionConfiguration -CollectionName $CollectionName -Security
        $collectionUserGroup = Get-RDSessionCollectionConfiguration -CollectionName $CollectionName -UserGroup
        @{
        "CollectionName" = $collectionGeneral.CollectionName;
        "ActiveSessionLimitMin" = $collectionConnection.ActiveSessionLimitMin;
        "AuthenticateUsingNLA" = $collectionSecurity.AuthenticateUsingNLA;
        "AutomaticReconnectionEnabled" = $collectionConnection.AutomaticReconnectionEnabled;
        "BrokenConnectionAction" = $collectionConnection.BrokenConnectionAction;
        "ClientDeviceRedirectionOptions" = $collectionClient.ClientDeviceRedirectionOptions;
        "ClientPrinterAsDefault" = $collectionClient.ClientPrinterAsDefault;
        "ClientPrinterRedirected" = $collectionClient.ClientPrinterRedirected;
        "CollectionDescription" = $collectionGeneral.CollectionDescription;
        "CustomRdpProperty" = $collectionGeneral.CustomRdpProperty;
        "DisconnectedSessionLimitMin" = $collectionGeneral.DisconnectedSessionLimitMin;
        "EncryptionLevel" = $collectionSecurity.EncryptionLevel;
        "IdleSessionLimitMin" = $collectionConnection.IdleSessionLimitMin;
        "MaxRedirectedMonitors" = $collectionClient.MaxRedirectedMonitors;
        "RDEasyPrintDriverEnabled" = $collectionClient.RDEasyPrintDriverEnabled;
        "SecurityLayer" = $collectionSecurity.SecurityLayer;
        "TemporaryFoldersDeletedOnExit" = $collectionConnection.TemporaryFoldersDeletedOnExit;
        "UserGroup" = $collectionUserGroup.UserGroup;
        }
}


########################################################################
# The Set-TargetResource cmdlet.
########################################################################
function Set-TargetResource

{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory)]
        [ValidateLength(1,15)]
        [string] $CollectionName,
        [uint32] $ActiveSessionLimitMin,
        [boolean] $AuthenticateUsingNLA,
        [boolean] $AutomaticReconnectionEnabled,
        [string] $BrokenConnectionAction,
        [string] $ClientDeviceRedirectionOptions,
        [boolean] $ClientPrinterAsDefault,
        [boolean] $ClientPrinterRedirected,
        [string] $CollectionDescription,
        [string] $ConnectionBroker,
        [string] $CustomRdpProperty,
        [uint32] $DisconnectedSessionLimitMin,
        [string] $EncryptionLevel,
        [uint32] $IdleSessionLimitMin,
        [uint32] $MaxRedirectedMonitors,
        [boolean] $RDEasyPrintDriverEnabled,
        [string] $SecurityLayer,
        [boolean] $TemporaryFoldersDeletedOnExit,
        [string] $UserGroup
    )
    Write-Verbose "Setting DSC collection properties"
    $discoveredCollectionName = Get-RDSessionCollection | % {Get-RDSessionHost $_.CollectionName} | ? {$_.SessionHost -ieq $localhost} | % {$_.CollectionName}
    if ($collectionName -ne $discoveredCollectionName) {$PSBoundParameters.collectionName = $discoveredCollectionName}
    Set-RDSessionCollectionConfiguration @PSBoundParameters
}


#######################################################################
# The Test-TargetResource cmdlet.
#######################################################################
function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory)]
        [ValidateLength(1,15)]
        [string] $CollectionName,
        [uint32] $ActiveSessionLimitMin,
        [boolean] $AuthenticateUsingNLA,
        [boolean] $AutomaticReconnectionEnabled,
        [string] $BrokenConnectionAction,
        [string] $ClientDeviceRedirectionOptions,
        [boolean] $ClientPrinterAsDefault,
        [boolean] $ClientPrinterRedirected,
        [string] $CollectionDescription,
        [string] $ConnectionBroker,
        [string] $CustomRdpProperty,
        [uint32] $DisconnectedSessionLimitMin,
        [string] $EncryptionLevel,
        [uint32] $IdleSessionLimitMin,
        [uint32] $MaxRedirectedMonitors,
        [boolean] $RDEasyPrintDriverEnabled,
        [string] $SecurityLayer,
        [boolean] $TemporaryFoldersDeletedOnExit,
        [string] $UserGroup
    )
    
    Write-Verbose "Testing DSC collection properties"
    $collectionName = Get-RDSessionCollection | % {Get-RDSessionHost $_.CollectionName} | ? {$_.SessionHost -ieq $localhost} | % {$_.CollectionName}
    $PSBoundParameters.Remove("Verbose") | out-null
    $PSBoundParameters.Remove("Debug") | out-null
    $PSBoundParameters.Remove("ConnectionBroker") | out-null
    $Check = $true

    $Get = Get-TargetResource -CollectionName $CollectionName
    $PSBoundParameters.keys | % {if ($PSBoundParameters[$_] -ne $Get[$_]) {$Check = $false} }
    $Check
}

Export-ModuleMember -Function *-TargetResource