/
RDSRemoteApp.psm1
277 lines (241 loc) · 9.96 KB
/
RDSRemoteApp.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
267
268
269
270
271
272
273
274
275
276
277
Import-Module RemoteDesktopServices
function New-RDSRemoteApp {
<#
.SYNOPSIS
Creates a new RemoteApp on Windows Server 2008 R2 RDS server the function is executed on.
.DESCRIPTION
Creates a new RemoteApp using the supplied parameters.
.PARAMETER Alias
Alias for the new RemoteApp. Accepts ValueFromPipeline and ValueFromPipelineByPropertyName.
.PARAMETER Applicationpath
Path to the executable file for for the new RemoteApp. This file must exist before creating the new RemoteApp.
Accepts ValueFromPipeline and ValueFromPipelineByPropertyName.
.PARAMETER Displayname
Displayname for the new RemoteApp. This is the application name the users will see. Accepts ValueFromPipeline and ValueFromPipelineByPropertyName.
.PARAMETER ShowinRDWebAccess
True or false. Determines if the RemoteApp should be visible in RD Web Access. Defaults to true if the parameter is omitted. Accepts ValueFromPipeline and ValueFromPipelineByPropertyName.
.PARAMETER UserAssignment
User Assignment for the new RemoteApp, determines which users have access to the new RemoteApp. If this parameter is omitted, all authenticated users have access.
The format must be username@DomainNetBIOSName, e.g. JohnDoe@CONTOSO and "Domain Users@CONTOSO". Separate entries by comma if more than one.
Accepts ValueFromPipeline and ValueFromPipelineByPropertyName.
.PARAMETER CommandlineSetting
0 = Do not allow command-line arguments, 1 = Allow any command-line arguments (not recommended), 2 = Always use the following command-line arguments
.PARAMETER CommandlineArguments
Command-line argument to be used when starting the new RemoteApp
.EXAMPLE
New-RDSRemoteApp -Alias Notepad -Applicationpath "%windir%\system32\notepad.exe" -Displayname Notepad -ShowinRDWebAccess $false -UserAssignment sales@CONTOSO,hr@CONTOSO
.EXAMPLE
New-RDSRemoteApp -Alias Calc -Applicationpath "%windir%\system32\calc.exe" -Displayname Calculator -CommandLineArgumentMode 2 -CommandlineArguments '/MyCustomParameter'
.NOTES
AUTHOR: Jan Egil Ring
BLOG: http://blog.powershell.no
LASTEDIT: 30.06.2010
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$Alias,
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$Applicationpath,
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$Displayname,
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[boolean]$ShowinRDWebAccess = $true,
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
$UserAssignment,
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[int]$CommandlineSetting,
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
$CommandlineArguments
)
#Create the new RemoteApp
if (Test-Path RDS:\RemoteApp\RemoteAppPrograms\$Alias) {
Write-Warning "The application $alias already exist!";return}
else {
New-Item -path RDS:\RemoteApp\RemoteAppPrograms -Name $Alias -applicationpath $Applicationpath | Out-Null
if (Test-Path RDS:\RemoteApp\RemoteAppPrograms\$Alias) {
Write-Host "The application $alias was succesfully created" -ForegroundColor yellow
}
}
#Configure RD Web Access visibility
if ($ShowinRDWebAccess){
Set-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias\ShowInWebAccess -Value 1
}
else
{
Set-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias\ShowInWebAccess -Value 0
}
#Configure Displayname
if ($Displayname) {
Set-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias\DisplayName -Value $Displayname
}
#Configure UserAssignment
if (($UserAssignment -and $UserAssignment.Length -gt 0) -or ($UserAssignment.UserAssignment -and $UserAssignment.UserAssignment.Length -gt 0)) {
if ($UserAssignment.UserAssignment){
$UserAssignment = $UserAssignment.UserAssignment.Split(",")
}
foreach ($item in $UserAssignment){
$item.UserAssignment
New-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias\UserAssignment -Name $item | Out-Null
}
}
#Configure CommandLineSetting
if ($CommandlineSetting -ne $null -and $CommandlineSetting -ne 2) {
Set-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias\CommandLineSetting -Value $CommandlineSetting -Force
}
#Configure CommandLineArguments
if ($CommandlineArguments -and ($CommandlineSetting -eq 2)) {
if ($CommandlineArguments.RequiredCommandline) {
$CommandlineArguments = $CommandlineArguments.RequiredCommandline
}
Set-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias\CommandLineSetting -Value $CommandlineSetting -RequiredCommandLine $CommandlineArguments -Force
}
}
function Import-RDSRemoteApps {
<#
.SYNOPSIS
Imports all RDS RemoteApps from the provided CSV-file to the Windows Server 2008 R2 RDS server the function is executed on.
.DESCRIPTION
Imports all RDS RemoteApps from the provided CSV-file to the Windows Server 2008 R2 RDS server the function is executed on,
using the function New-RDSRemoteApp. One mandatory parameter: Path
.PARAMETER path
Path to the CSV-file to be imported
.EXAMPLE
Import-RDSRemoteApps -Path C:\temp\RemoteApps.csv
Imports all RemoteApps from the specified CSV-file.
.NOTES
AUTHOR: Jan Egil Ring
BLOG: http://blog.powershell.no
LASTEDIT: 30.06.2010
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[string]$Path
)
foreach ($app in (Import-Csv $path)){
$app | New-RDSRemoteApp
}
}
function Export-RDSRemoteApps {
<#
.SYNOPSIS
Exports all RDS RemoteApps from the Windows Server 2008 R2 RDS server the function is executed on.
.DESCRIPTION
Exports all RDS RemoteApps from the Windows Server 2008 R2 RDS server the function is executed on to a CSV-file.
One mandatory parameter: Path
.PARAMETER path
Path to the CSV-file to be exported
.EXAMPLE
Export-RDSRemoteApps -Path C:\temp\RemoteApps.csv
Exports all RemoteApps to the specified CSV-file.
.NOTES
AUTHOR: Jan Egil Ring
BLOG: http://blog.powershell.no
LASTEDIT: 30.06.2010
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[string]$Path
)
BEGIN {
$collection = @()
$RemoteApps = Get-ChildItem -path RDS:\RemoteApp\RemoteAppPrograms
}
PROCESS {
foreach ($app in $RemoteApps) {
$exportobject = "" | Select-Object Alias,Displayname,Applicationpath,UserAssignment,CommandLineSetting,RequiredCommandLine
$exportobject.Alias = $app.Name
$exportobject.Displayname = (Get-ChildItem -path RDS:\RemoteApp\RemoteAppPrograms\$app\DisplayName).CurrentValue
$exportobject.Applicationpath = (Get-ChildItem -path RDS:\RemoteApp\RemoteAppPrograms\$app\Path).CurrentValue
$exportobject.CommandLineSetting = (Get-ChildItem -path RDS:\RemoteApp\RemoteAppPrograms\$app\CommandLineSetting).CurrentValue
$RequiredCommandLineValue = (Get-ChildItem -path RDS:\RemoteApp\RemoteAppPrograms\$app\RequiredCommandLine).CurrentValue
if ($RequiredCommandLineValue){
$exportobject.RequiredCommandLine = $RequiredCommandLineValue
}
$UAarray = @()
$UA = Get-ChildItem -Path RDS:\RemoteApp\RemoteAppPrograms\$app\UserAssignment
foreach ($u in $UA) {
$UAarray += $u.name
$exportobject.UserAssignment = [system.string]::Join(",",$UAarray)
}
$collection += $exportobject
}
}
END {
if (-not (Test-Path $path)) {
New-Item -ItemType file -Path $path | Out-Null
}
$collection | Export-Csv -Path $path -NoTypeInformation
}
}
function Remove-RDSRemoteApp {
<#
.SYNOPSIS
Removes the specified RemoteApp from the Windows Server 2008 R2 RDS server the function is executed on.
.DESCRIPTION
Removes the specified RemoteApp from the Windows Server 2008 R2 RDS server the function is executed on. One mandatory parameter: Alias
.PARAMETER Alias
The alias of the application to be removed
.EXAMPLE
Remove-RDSRemoteApp -Alias Calc
Removes the Calc RemoteApp.
.EXAMPLE
Get-RDSRemoteApp | Foreach-Object {Remove-RDSRemoteApp $_.Alias}
Removes all RemoteApps.
.NOTES
AUTHOR: Jan Egil Ring
BLOG: http://blog.powershell.no
LASTEDIT: 30.06.2010
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$Alias
)
#Remove the specified RemoteApp
if (-not (Test-Path RDS:\RemoteApp\RemoteAppPrograms\$Alias)) {
Write-Warning "The application $alias doesn`t exist!";return}
else {
Remove-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias -Recurse -Force -Confirm:$false | Out-Null
if (-not (Test-Path RDS:\RemoteApp\RemoteAppPrograms\$Alias)) {
Write-Host "The application $alias was successfully removed" -ForegroundColor yellow
}
}
}
function Get-RDSRemoteApp {
<#
.SYNOPSIS
Retrieves info about the specified RemoteApp from the Windows Server 2008 R2 RDS server the function is executed on.
.DESCRIPTION
Retrieves info about specified RemoteApp from the Windows Server 2008 R2 RDS server the function is executed on. One optional parameter: Alias
If Alias is omitted, all RemoteApps are returned.
.PARAMETER Alias
The alias of the application to be retirived
.EXAMPLE
Get-RDSRemoteApp -Alias Calc
.NOTES
AUTHOR: Jan Egil Ring
BLOG: http://blog.powershell.no
LASTEDIT: 30.06.2010
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$false)]
[string]$Alias
)
if ($Alias) {
if (-not (Test-Path RDS:\RemoteApp\RemoteAppPrograms\$Alias))
{
Write-Warning "The application $alias doesn`t exist!";return
}
Get-Item -path RDS:\RemoteApp\RemoteAppPrograms\$Alias | Select-Object @{"Name"="Displayname";"Expression"={(Get-Item -path "RDS:\RemoteApp\RemoteAppPrograms\$Alias\DisplayName").CurrentValue}},@{"Name"="Alias";"Expression"={$Alias}},@{"Name"="Path";"Expression"={(Get-Item -path "RDS:\RemoteApp\RemoteAppPrograms\$Alias\Path").CurrentValue}}
}
else
{
foreach ($RemoteApp in (Get-ChildItem -path RDS:\RemoteApp\RemoteAppPrograms)) {
$RemoteApp | Select-Object @{"Name"="Displayname";"Expression"={(Get-Item -path "RDS:\RemoteApp\RemoteAppPrograms\$RemoteApp\DisplayName").CurrentValue}},@{"Name"="Alias";"Expression"={($RemoteApp).Name}},@{"Name"="Path";"Expression"={(Get-Item -path "RDS:\RemoteApp\RemoteAppPrograms\$RemoteApp\Path").CurrentValue}}
}
}
}