forked from JamesKindon/Citrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateDesktopsForVDAs.ps1
328 lines (283 loc) · 11.7 KB
/
CreateDesktopsForVDAs.ps1
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<#
.SYNOPSIS
Creates a Tag per VDA and Creates a dedicated desktop to launch only against that Tag
.DESCRIPTION
Creates a Tag per VDA and Creates a dedicated desktop to launch only against that Tag.
Original Script by Martin Zugec. Original detail: https://www.citrix.com/blogs/2017/04/17/how-to-assign-desktops-to-specific-servers-in-xenapp-7/
Updated by James Kindon
.PARAMETER DesktopGroupName
Desktop Group name to target. Defaults to * all multi-session Desktop Grooups
.PARAMETER UserGroups
Array of User Groups to assign the desktop too
.PARAMETER TagPrefix
Tag Prefix. Defaults to ServerTag_. End result will be for example: ServerTag_Server01
.PARAMETER DesktopSuffix
Suffix for the name of the desktop. Handy if you already have desktops with the server name in use. Defaults to _Admin. End result will be for example: ServerTag_Server01_Admin
.PARAMETER RemoveBrokerTags
Removes all Tags that this script may have created - good for testing and wiping
.PARAMETER RemoveDesktops
Removes all published desktops that this script may have created - good for testing and wiping
.PARAMETER LogPath
Logpath output for all operations
.PARAMETER LogRollover
Number of days before logfiles are rolled over. Default is 5
.EXAMPLE
.\CreateDesktopsForVDAs.ps1 -Usergroups "KINDO\Group1"
Creates a Desktop for each multi session VDA in all Desktop Groups. Creates and Assigns a Tag in the default format of ServerTag_ServerName. Creates a Desktop with the forma of "ServerName_Admin". Assigns to "KINDO\Group1"
.EXAMPLE
.\CreateDesktopsForVDAs.ps1 -Usergroups "KINDO\Group1" -DesktopGroupName "DG1"
Creates a Desktop for each multi session VDA in the DG1 Desktop Group. Creates and Assigns a Tag in the default format of "ServerTag_ServerName". Creates a Desktop with the forma of "ServerName_Admin". Assigns to "KINDO\Group1"
.EXAMPLE
.\CreateDesktopsForVDAs.ps1 -Usergroups "KINDO\Group1" -TagPrefix "Bob_" -DesktopSuffix "_Burt"
Creates a Desktop for each multi session VDA in all Desktop Groups. Creates and Assigns a Tag in the format of "Bob_ServerName". Creates a Desktop with the format of "ServerName_Burt" Assigns to "KINDO\Group1"
.EXAMPLE
.\CreateDesktopsForVDAs.ps1 -RemoveBrokerTags -TagPrefix "Bob_" -RemoveDesktops -DesktopSuffix "_Burt"
Removes all desktops with the desktop name "ServerName_Burt". Removes all tags with the Tag Names "Bob_ServerName"
#>
#region Params
# ============================================================================
# Parameters
# ============================================================================
Param(
[Parameter(Mandatory = $false)]
[string]$LogPath = "C:\Logs\DesktopPerServer.log",
[Parameter(Mandatory = $false)]
[int]$LogRollover = 5, # number of days before logfile rollover occurs
[Parameter(Mandatory = $false)]
[string]$DesktopGroupName = "*", # Desktop Groups - Defaults to all
[Parameter(Mandatory = $True)]
[Array]$UserGroups = "", # User Groups to assign tags - Typically an Admin Group
[Parameter(Mandatory = $false)]
[string]$TagPrefix = "ServerTag_", # Prefix for the Tag name
[Parameter(Mandatory = $false)]
[string]$DesktopSuffix = "_Admin", # Suffix for the Desktop
[Parameter(Mandatory = $false)]
[switch]$RemoveBrokerTags,
[Parameter(Mandatory = $false)]
[switch]$RemoveDesktops
)
#endregion
#region Functions
# ============================================================================
# Functions
# ============================================================================
function Write-Log {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[Alias("LogContent")]
[string]$Message,
[Parameter(Mandatory = $false)]
[Alias('LogPath')]
[string]$Path = $LogPath,
[Parameter(Mandatory = $false)]
[ValidateSet("Error", "Warn", "Info")]
[string]$Level = "Info",
[Parameter(Mandatory = $false)]
[switch]$NoClobber
)
Begin {
# Set VerbosePreference to Continue so that verbose messages are displayed.
$VerbosePreference = 'Continue'
}
Process {
# If the file already exists and NoClobber was specified, do not write to the log.
if ((Test-Path $Path) -AND $NoClobber) {
Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
Return
}
# If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
elseif (!(Test-Path $Path)) {
Write-Verbose "Creating $Path."
$NewLogFile = New-Item $Path -Force -ItemType File
}
else {
# Nothing to see here yet.
}
# Format Date for our Log File
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Write message to error, warning, or verbose pipeline and specify $LevelText
switch ($Level) {
'Error' {
Write-Error $Message
$LevelText = 'ERROR:'
}
'Warn' {
Write-Warning $Message
$LevelText = 'WARNING:'
}
'Info' {
Write-Verbose $Message
$LevelText = 'INFO:'
}
}
# Write log entry to $Path
"$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
}
End {
}
}
function Start-Stopwatch {
Write-Log -Message "Starting Timer" -Level Info
$Global:StopWatch = [System.Diagnostics.Stopwatch]::StartNew()
}
function Stop-Stopwatch {
Write-Log -Message "Stopping Timer" -Level Info
$StopWatch.Stop()
if ($StopWatch.Elapsed.TotalSeconds -le 1) {
Write-Log -Message "Script processing took $($StopWatch.Elapsed.TotalMilliseconds) ms to complete." -Level Info
}
else {
Write-Log -Message "Script processing took $($StopWatch.Elapsed.TotalSeconds) seconds to complete." -Level Info
}
}
function RollOverlog {
$LogFile = $LogPath
$LogOld = Test-Path $LogFile -OlderThan (Get-Date).AddDays(-$LogRollover)
$RolloverDate = (Get-Date -Format "dd-MM-yyyy")
if ($LogOld) {
Write-Log -Message "$LogFile is older than $LogRollover days, rolling over" -Level Info
$NewName = [io.path]::GetFileNameWithoutExtension($LogFile)
$NewName = $NewName + "_$RolloverDate.log"
Rename-Item -Path $LogFile -NewName $NewName
Write-Log -Message "Old logfile name is now $NewName" -Level Info
}
}
function ImportModule {
param (
[Parameter(Mandatory = $True)]
[String]$ModuleName
)
Write-Log -Message "Importing $ModuleName Module" -Level Info
try {
Import-Module -Name $ModuleName -Force -ErrorAction Stop
}
catch {
Write-Log -Message "Failed to Import $ModuleName Module. Exiting" -Level Warn
StopIteration
Exit 1
}
}
function StartIteration {
Write-Log -Message "--------Starting Iteration--------" -Level Info
RollOverlog
Start-Stopwatch
}
function StopIteration {
Stop-Stopwatch
Write-Log -Message "--------Finished Iteration--------" -Level Info
}
function CreateTag {
try {
Write-Log -Message "Creating Tag: $m_TagName" -Level Info
$null = New-BrokerTag -Name $m_TagName -Description "Tag used to restrict resources to machine $($m_VDA.MachineName)" -ErrorAction Stop
}
catch {
Write-Log -Message $_ -Level Warn
}
}
function AssignTagToMachine {
if ((Get-BrokerMachine -MachineName $m_VDA.MachineName).Tags -match "$m_TagName") {
Write-Log -Message "Tag: $($m_TagName) already assigned to machine" -Level Info
}
else {
try {
Write-Log -Message "Assigning Tag: $($m_TagName) to Machine: $($m_VDA.MachineName)" -Level Info
Add-BrokerTag -Name (Get-BrokerTag -Name $m_TagName).Name -Machine $m_VDA.MachineName -ErrorAction Stop
}
catch {
Write-Log -Message $_ -Level Warn
}
}
}
function CreateDesktop {
# Create new entitlement policy rule
if (Get-BrokerEntitlementPolicyRule -Name "$($m_SimpleMachineName)$($DesktopSuffix)" -ErrorAction SilentlyContinue) {
Write-Log -Message "Desktop: $($m_SimpleMachineName)$($DesktopSuffix) already exists" -Level Info
}
else {
try {
Write-Log -Message "Creating Desktop: $($m_SimpleMachineName)$($DesktopSuffix) and restricting to Tag: $m_TagName" -Level Info
$null = New-BrokerEntitlementPolicyRule "$($m_SimpleMachineName)$($DesktopSuffix)" -DesktopGroupUid $m_VDA.DesktopGroupUid -IncludedUsers $UserGroups -PublishedName $m_SimpleMachineName -RestrictToTag $m_TagName -ErrorAction Stop
}
catch {
Write-Log -Message $_ -Level Warn
}
}
}
function RemoveBrokerTags {
$Hosts = Get-BrokerMachine -SessionSupport MultiSession -DesktopGroupName $DesktopGroupName
foreach ($Machine in $Hosts) {
$m_SimpleMachineName = $($Machine.MachineName.Split('\')[1])
$m_TagName = "$($TagPrefix)$($m_SimpleMachineName)"
if ($m_TagName -in $Machine.Tags ) {
Write-Log -Message "Machine: $($Machine.MachineName) with Tag: $($m_TagName) found. Removing" -Level Info
Remove-BrokerTag -Name $m_TagName -Machine $Machine.MachineName
}
else {
Write-Log -Message "Machine: $($Machine.MachineName) does not have a Tag to remove" -Level Info
}
}
}
function RemoveDesktops {
$Hosts = Get-BrokerMachine -SessionSupport MultiSession -DesktopGroupName $DesktopGroupName
foreach ($Machine in $Hosts) {
[String]$m_SimpleMachineName = $($Machine.MachineName.Split('\')[1])
$DesktopPolicy = Get-BrokerEntitlementPolicyRule -Name "$($m_SimpleMachineName)$($DesktopSuffix)" -ErrorAction SilentlyContinue
if ($DesktopPolicy) {
try {
Write-Log -Message "Desktop: $($DesktopPolicy.Name) found. Removing" -Level Info
Remove-BrokerEntitlementPolicyRule $DesktopPolicy.Name -ErrorAction Stop
}
catch {
Write-Log -Message $_ -Level Warn
}
}
}
}
#endregion
#Region Execute
# ============================================================================
# Execute
# ============================================================================
StartIteration
Add-PSSnapin Citrix*
if ($RemoveBrokerTags.IsPresent) {
RemoveBrokerTags
}
if ($RemoveDesktops.IsPresent) {
RemoveDesktops
}
if ($RemoveBrokerTags.IsPresent -or $RemoveDesktops.IsPresent) {
StopIteration
Exit 0
}
$Hosts = Get-BrokerMachine -SessionSupport MultiSession -DesktopGroupName $DesktopGroupName
$Count = 1
Write-Log -Message "Processing $($Hosts.Count) Multi-Session hosts" -Level Info
ForEach ($m_VDA in $(Get-BrokerMachine -SessionSupport MultiSession -DesktopGroupName $DesktopGroupName)) {
Write-Log "Processing Host $($Count) of $($Hosts.Count)"
[String]$m_SimpleMachineName = $($m_VDA.MachineName.Split('\')[1])
[String]$m_TagName = "$($TagPrefix)$($m_SimpleMachineName)"
# Tag Creation Logic
if (Get-BrokerTag -Name $m_TagName -ErrorAction SilentlyContinue) {
Write-Log -Message "Tag: $($m_TagName) Exists" -Level Info
AssignTagToMachine
}
else {
CreateTag
}
# Tag Assignment Logic
If ($m_VDA.Tags -notcontains $m_TagName) {
AssignTagToMachine
}
# Desktop Creation Logic
CreateDesktop
$Count ++
}
StopIteration
Exit 0
#endregion