DSCResources/MSFT_xRDRemoteApp/MSFT_xRDRemoteApp.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
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 = "Tenant",
        [parameter(Mandatory)]
        [string] $DisplayName = "Calculator",
        [parameter(Mandatory)]
        [string] $FilePath = "C:\Windows\System32\calc.exe",
        [parameter(Mandatory)]
        [string] $Alias = "calc",
        [string] $FileVirtualPath,
        [string] $FolderName,
        [string] $CommandLineSetting,
        [string] $RequiredCommandLine,
        [uint32] $IconIndex,
        [string] $IconPath,
        [string] $UserGroups,
        [boolean] $ShowInWebAccess
    )
        Write-Verbose "Getting published RemoteApp program $DisplayName, if one exists."
        $CollectionName = Get-RDSessionCollection | % {Get-RDSessionHost $_.CollectionName} | ? {$_.SessionHost -ieq $localhost} | % {$_.CollectionName}
        $remoteApp = Get-RDRemoteApp -CollectionName $CollectionName -DisplayName $DisplayName -Alias $Alias

        @{
        "CollectionName" = $remoteApp.CollectionName;
        "DisplayName" = $remoteApp.DisplayName;
        "FilePath" = $remoteApp.FilePath;
        "Alias" = $remoteApp.Alias;
        "FileVirtualPath" = $remoteApp.FileVirtualPath;
        "FolderName" = $remoteApp.FolderName;
        "CommandLineSetting" = $remoteApp.CommandLineSetting;
        "RequiredCommandLine" = $remoteApp.RequiredCommandLine;
        "IconIndex" = $remoteApp.IconIndex;
        "IconPath" = $remoteApp.IconPath;
        "UserGroups" = $remoteApp.UserGroups;
        "ShowInWebAccess" = $remoteApp.ShowInWebAccess;
        }
}


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

{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory)]
        [ValidateLength(1,15)]
        [string] $CollectionName,
        [parameter(Mandatory)]
        [string] $DisplayName,
        [parameter(Mandatory)]
        [string] $FilePath,
        [parameter(Mandatory)]
        [string] $Alias,
        [string] $FileVirtualPath,
        [string] $FolderName,
        [string] $CommandLineSetting,
        [string] $RequiredCommandLine,
        [uint32] $IconIndex,
        [string] $IconPath,
        [string] $UserGroups,
        [boolean] $ShowInWebAccess
    )
    Write-Verbose "Making updates to RemoteApp."
    $CollectionName = Get-RDSessionCollection | % {Get-RDSessionHost $_.CollectionName} | ? {$_.SessionHost -ieq $localhost} | % {$_.CollectionName}
    $PSBoundParameters.collectionName = $CollectionName
    if (!$(Get-RDRemoteApp -Alias $Alias)) {
        New-RDRemoteApp @PSBoundParameters
        }
    else {
        Set-RDRemoteApp @PSBoundParameters
    }
}


#######################################################################
# The Test-TargetResource cmdlet.
#######################################################################
function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory)]
        [ValidateLength(1,15)]
        [string] $CollectionName,
        [parameter(Mandatory)]
        [string] $DisplayName,
        [parameter(Mandatory)]
        [string] $FilePath,
        [parameter(Mandatory)]
        [string] $Alias,
        [string] $FileVirtualPath,
        [string] $FolderName,
        [string] $CommandLineSetting,
        [string] $RequiredCommandLine,
        [uint32] $IconIndex,
        [string] $IconPath,
        [string] $UserGroups,
        [boolean] $ShowInWebAccess
    )
    Write-Verbose "Testing if RemoteApp is published."
    $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 -DisplayName $DisplayName -FilePath $FilePath -Alias $Alias
    $PSBoundParameters.keys | % {if ($PSBoundParameters[$_] -ne $Get[$_]) {$Check = $false} }
    $Check
}

Export-ModuleMember -Function *-TargetResource