Citrix – Change wallpaper with powershell on a Citrix published desktop or Microsoft remote desktop

Summary

You publish Desktops with a Citrix XenApp/XenDesktop/Workspace or Microsoft Remote Desktop Services (RDS) environment and restrict your users with GPO to change any display settings, but your users want to change the background image/wallpaper?

Here is a method with PowerShell, that gives your users the flexibility back to upload an own  background image/wallpapers or choose from three default background wallpapers.

Description

The PowerShell script and the required images are saved in a subfolder in the program files path and can be started through a start menu or desktop shortcut. The PowerShell script generate a user friendly GUI that allows the users to upload an own image/wallpaper or choose from three different and pre- defined wallpapers.

You can find the complete Script and image structure here => SetWallpaper_20171004.

OK, let’s start…

First you must declare some variables and values to understand and use the following PowerShell script.

  • Location of the PowerShell script (script line 32) $ProgramPath (In my scenario, the powershell script and the images are saved in the program files path.)
  • Upload location for the own wallpaper (script line 33) $ProfilePath (Path to save the uploaded wallpaper image and the generated base64 thumbnail.)
  • Max file length for upload (script line 34) $MaxFileLength (restrict file size for upload.)
  • Wallpaper icon (script line 37) $WallpaperIcon (Filename of the icon, that show in the upper left corner of the PowerShell GUI.)
  • Wallpaper style (script line 38) $WallpaperStyle (Stretch or center the wallpaper)
  • Name of wallpaper file (script line 39-41) $WallpaperPict1, $WallpaperPict2, $WallpaperPict3 (The names of the wallpaper image files)

    The Features of the PowerShell script

  • Three different pre-defined background wallpapers to choose
  • Upload and set own background image/wallpaper
  • Automatically generate a thumbnail from own uploaded image/wallpaper as base64 file in the roaming user profile (Why a base64 file; The thumbnail must be changed while the PowerShell form are active and loaded. A picture are locked by the loaded PowerShell form and cannot be deleted or changed, but a base64 file can be deleted and changed when the PowerShell form are loaded.)
  • File size limitation for own uploaded image/wallpaper
  • Graphical user interface (GUI)
  • The PowerShell script read the actual session resolution and set the most fitting image/wallpaper from the source files

The GUI

The GUI are simple and easy to use.

Start PowerShell GUI over an desktop or start menu shortcut.
PowerShell GUI overview.

 Assistant to upload own background image/wallpaper.
 
Automatically generate a thumbnail from own uploaded image/wallpaper as base64 file in the roaming user profile.
 
 Thumbnail preview and set own background image/wallpaper.
 File size limitation for own uploaded image/wallpaper (the default file size upload limitation are 1MB and can be changed in the PowerShell script variables).
 Three different pre-defined background wallpapers to choose.

The PowerShell script

The script set your own uploaded or one of the three pre-defined background images/wallpaper on a Citrix published Desktop or Microsoft remote desktop.
Lines: 478

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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
####################################################################################
#  Title:       Set wallpaper on Citrix RDS
#  Description: Set wallpaper in user session on Citrix RDS server and upload own
#               wallpaper
#  Version:     1.3/roe
#  Date/Time:   02.10.2017 / 21:24
#  Developer:   Beat
####################################################################################

#----------------------------------------------
# Collect and define data for form
#----------------------------------------------
function OnApplicationLoad {
return $true
}
function OnApplicationExit {
$script:ExitCode = 0
}

#----------------------------------------------
# Start function Generat form
#----------------------------------------------
function GenerateForm {
#Import form assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

#***********************************************
#Define variables
#***********************************************
$UserName = $env:USERNAME
$ProgramPath = "c:\Program Files\CTXTools\Scripts\SetWallpaper"
$ProfilePath = "c:\Users\$UserName\AppData\Roaming"
$MaxFileLength = "1000kb"
$ThumbHeight = "152"
$ThumbLength = "95"
$WallpaperStyle = 2 #(Stretch=2, Center=1)
$WallpaperIcon = "wallpaper.ico"
$WallpaperPict1 = "wallpaperctx"
$WallpaperPict2 = "wallpapercompany"
$WallpaperPict3 = "wallpapercompany2"
#***********************************************

#Define thumbnail path
$WallpaperThumb1 = "thumb$WallpaperPict1.jpg"
$WallpaperThumb2 = "thumb$WallpaperPict2.jpg"
$WallpaperThumb3 = "thumb$WallpaperPict3.jpg"
$WallpaperThumbOwn = "thumb$WallpaperOwn.jpg"
$wallpaper1 = (get-item "$ProgramPath\pict\$WallpaperThumb1")
$wallpaper2 = (get-item "$ProgramPath\pict\$WallpaperThumb2")
$wallpaper3 = (get-item "$ProgramPath\pict\$WallpaperThumb3")
$WallpaperOwn = "wallpaperown"

#Define Citrix session ID
$keySessionnr = "HKCU:\SOFTWARE\Citrix\SessionSfr"
$CitrixSessionID = (Get-ChildItem -Path $keySessionnr -Name)

#Define Citrix session display resolution
$keyResolution = "HKLM:\SOFTWARE\Citrix\Ica\Session\$CitrixSessionID\Connection"
$CitrixDispHRes = (Get-ItemProperty -Path $keyResolution -Name "HRes" | Select -exp HRes)
$CitrixDispVRes = (Get-ItemProperty -Path $keyResolution -Name "VRes" | Select -exp VRes)

#Generated form objects
$Icon = New-Object system.drawing.icon ("$ProgramPath\pict\$WallpaperIcon")
$Font = New-Object System.Drawing.Font("Microsoft Sans Serif",10,[System.Drawing.FontStyle]::Regular)
$labelHead = New-Object System.Windows.Forms.Label
$img1 = [System.Drawing.Image]::Fromfile($wallpaper1);
$img2 = [System.Drawing.Image]::Fromfile($wallpaper2);
$img3 = [System.Drawing.Image]::Fromfile($wallpaper3);
$form1 = New-Object System.Windows.Forms.Form
$buttonupl = New-Object System.Windows.Forms.Button
$buttonimg1 = New-Object System.Windows.Forms.Button
$buttonimg2 = New-Object System.Windows.Forms.Button
$buttonimg3 = New-Object System.Windows.Forms.Button
$buttonimgOwn = New-Object System.Windows.Forms.Button
$label1 = New-Object System.Windows.Forms.Label
$label2 = New-Object System.Windows.Forms.Label
$labelupl = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$FontFoot = New-Object System.Drawing.Font("Microsoft Sans Serif",7,[System.Drawing.FontStyle]::Regular)
$labelFoot = New-Object System.Windows.Forms.Label

#Import own image from base64 code and generate form object
$base64ImageString = [IO.File]::ReadAllText("$ProfilePath\wallpaperownthumb.txt")
$imageBytes = [Convert]::FromBase64String($base64ImageString)
$ms = New-Object IO.MemoryStream($imageBytes, 0, $imageBytes.Length)
$ms.Write($imageBytes, 0, $imageBytes.Length);
$imgOwn = [System.Drawing.Image]::FromStream($ms, $true)

#----------------------------------------------
# Call "set wallpaper" function
#----------------------------------------------
#Define script blocks for function "set wallpaper"
$ScriptBlockWallpaperAPI = {
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Tile, Center, Stretch, NoChange
}
public class Setter {
public const int SetDesktopWallpaper = 20;
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
[DllImport("
user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
public static void SetWallpaper ( string path, Wallpaper.Style style ) {
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
RegistryKey key = Registry.CurrentUser.OpenSubKey("
Control Panel\\Desktop", true);
switch( style )
{
case Style.Stretch :
key.SetValue(@"
WallpaperStyle", "2") ;
key.SetValue(@"
TileWallpaper", "0") ;
break;
case Style.Center :
key.SetValue(@"
WallpaperStyle", "1") ;
key.SetValue(@"
TileWallpaper", "0") ;
break;
case Style.Tile :
key.SetValue(@"
WallpaperStyle", "1") ;
key.SetValue(@"
TileWallpaper", "1") ;
break;
case Style.NoChange :
break;
}
key.Close();
}
}
}
"
@
#End ScriptBlockWallpaperAPI
}

$ScriptBlockSetWallpaper = {param ($WallpaperPict)
If ($CitrixDispHRes -lt 2561)
{
If ($CitrixDispHRes -lt 1921)
{
If ($CitrixDispHRes -lt 1681)
{
If ($CitrixDispHRes -lt 1441)
{
[Wallpaper.Setter]::SetWallpaper( "$ProgramPath\pict\$WallpaperPict"+"_1440_900.jpg", $WallpaperStyle )
Write-Host "1440_900"
}
else
{
[Wallpaper.Setter]::SetWallpaper( "$ProgramPath\pict\$WallpaperPict"+"_1680_1050.jpg", $WallpaperStyle )
Write-Host "1680_1050"
}
}
else
{
[Wallpaper.Setter]::SetWallpaper( "$ProgramPath\pict\$WallpaperPict"+"_1920_1200.jpg", $WallpaperStyle )
Write-Host "1920_1200"
}

}
else
{
[Wallpaper.Setter]::SetWallpaper( "$ProgramPath\pict\$WallpaperPict"+"_2560_1440.jpg", $WallpaperStyle )
Write-Host "2560_1440"
}
}
else
{
[Wallpaper.Setter]::SetWallpaper( "$ProgramPath\pict\$WallpaperPict"+"_2560_1440.jpg", $WallpaperStyle )
}
#End ScriptBlockSetWallpaper
}

function wallpaperOwn {
& $ScriptBlockWallpaperAPI
[Wallpaper.Setter]::SetWallpaper( "$ProfilePath\$WallpaperOwn.jpg", $WallpaperStyle )
}

function wallpaper1 {
& $ScriptBlockWallpaperAPI
& $ScriptBlockSetWallpaper -WallpaperPict $WallpaperPict1
}

function wallpaper2 {
& $ScriptBlockWallpaperAPI
& $ScriptBlockSetWallpaper -WallpaperPict $WallpaperPict2
}

function wallpaper3 {
& $ScriptBlockWallpaperAPI
& $ScriptBlockSetWallpaper -WallpaperPict $WallpaperPict3
}

#----------------------------------------------
# Call "make thumb" function
#----------------------------------------------
function makethumb
{
Param([Parameter(Mandatory=$true)][string]$InputFile, [string]$OutputFile, [int32]$Width, [int32]$Height, [int32]$Scale, [Switch]$Display)

#Add system drawing assembly
Add-Type -AssemblyName System.Drawing

#Open image file
$img = [System.Drawing.Image]::FromFile((Get-Item $InputFile))

#Define new resolution
if($Width -gt 0) { [int32]$new_width = $Width }
elseif($Scale -gt 0) { [int32]$new_width = $img.Width * ($Scale / 100) }
else { [int32]$new_width = $img.Width / 2 }
if($Height -gt 0) { [int32]$new_height = $Height }
elseif($Scale -gt 0) { [int32]$new_height = $img.Height * ($Scale / 100) }
else { [int32]$new_height = $img.Height / 2 }

#Create empty canvas for the new image
$img2 = New-Object System.Drawing.Bitmap($new_width, $new_height)

#Draw new image on the empty canvas
$graph = [System.Drawing.Graphics]::FromImage($img2)
$graph.DrawImage($img, 0, 0, $new_width, $new_height)

#Create window to display the new image
if($Display)
{
Add-Type -AssemblyName System.Windows.Forms
$win = New-Object Windows.Forms.Form
$box = New-Object Windows.Forms.PictureBox
$box.Width = $new_width
$box.Height = $new_height
$box.Image = $img2
$win.Controls.Add($box)
$win.AutoSize = $true
$win.ShowDialog()
}

#Save the image
if($OutputFile -ne "")
{
$img2.Save($OutputFile);
}
}

#----------------------------------------------
# Call "upload own wallpaper" function
#----------------------------------------------
function wallpaperupload($initialDirectory)
{
#Remove previous image
Remove-Item "$ProfilePath\$WallpaperOwn.jpg" -force
Remove-Item "$ProfilePath\wallpaperownthumb.txt" -force

#Create new file dialog
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = “All files (*.jpg)| *.jpg”
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
$Fileuplname = $OpenFileDialog.filename

#Open walpaperupload handle and check file length
if ((Get-Item $Fileuplname).length -lt $MaxFileLength)
{
Copy-Item "$Fileuplname" -Destination "$ProfilePath\$WallpaperOwn.jpg"
makethumb -InputFile "$ProfilePath\$WallpaperOwn.jpg" -Width $ThumbHeight -Height $ThumbLength -OutputFile "$ProfilePath\$WallpaperThumbOwn"
[convert]::ToBase64String((Get-Content "$ProfilePath\$WallpaperThumbOwn" -Encoding byte))>>"$ProfilePath\wallpaperownthumb.txt"
Remove-Item "$ProfilePath\$WallpaperThumbOwn" -force
}
else
{
Add-Type -AssemblyName PresentationCore,PresentationFramework
[System.Windows.Forms.MessageBox]::Show("The file size exceeds the maximum size of 1MB.","File size exceeded",0,[System.Windows.Forms.MessageBoxIcon]::Error)
}

$form1.Dispose()
GenerateForm
}

#----------------------------------------------
# Call "form functions" from button click
#----------------------------------------------
#Call funtion from ButtonUpload
$handler_buttonupl_Click= ${function:wallpaperupload}
{

#Call funtion from ButtonOwn
}
$handler_buttonOwn_Click= ${function:wallpaperOwn}
{

#Call funtion from Button1
}
$handler_button1_Click= ${function:wallpaper1}
{

#Call funtion from Button2
}
$handler_button2_Click= ${function:wallpaper2}
{

#Call funtion from Button3
}
$handler_button3_Click= ${function:wallpaper3}
{
}

#----------------------------------------------
# Form code to generate gui
#----------------------------------------------
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 373
$System_Drawing_Size.Width = 516
$form1.Icon = $Icon
$form1.Font = $Font
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Set Wallpaper"
$form1.FormBorderStyle = "Fixed3D"

$labelHead.Text = "To set a wallpaper on your published Desktop, choose a default wallpaper or upload your own wallpaper."
$labelHead.Location = "15, 15"
$labelHead.Height = 45
$labelHead.Width = 330

$buttonupl.DataBindings.DefaultDataSourceUpdateMode = 0
$buttonupl.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,1,3,1)
$buttonimgOwn.DataBindings.DefaultDataSourceUpdateMode = 0
$buttonimgOwn.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,1,3,1)
$buttonimg1.DataBindings.DefaultDataSourceUpdateMode = 0
$buttonimg1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,1,3,1)
$buttonimg2.DataBindings.DefaultDataSourceUpdateMode = 0
$buttonimg2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,1,3,1)
$buttonimg3.DataBindings.DefaultDataSourceUpdateMode = 0
$buttonimg3.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,1,3,1)

$System_Drawing_Upload = New-Object System.Drawing.Point
$System_Drawing_Upload.X = 15
$System_Drawing_Upload.Y = 65
$buttonupl.Location = $System_Drawing_Upload
$buttonupl.Name = "Wallpaper1"
$System_Drawing_Upload = New-Object System.Drawing.Size
$System_Drawing_Upload.Height = 49
$System_Drawing_Upload.Width = 152
$buttonupl.Size = $System_Drawing_Upload
$buttonupl.TabIndex = 0
$buttonupl.Text = "Upload..."
$buttonupl.UseVisualStyleBackColor = $True
$buttonupl.add_Click($handler_buttonupl_Click)

$labelupl.Text = "Max file size: 1MB"
$labelupl.Location = "15, 128"
$labelupl.Height = 20
$labelupl.Width = 250

$label1.Text = "Citrix Session Resolution: $CitrixDispHRes/$CitrixDispVRes"
$label1.Location = "15, 143"
$label1.Height = 20
$label1.Width = 250

$System_Drawing_PointOwn = New-Object System.Drawing.Point
$System_Drawing_PointOwn.X = 350
$System_Drawing_PointOwn.Y = 128
$buttonimgOwn.Location = $System_Drawing_PointOwn
$buttonimgOwn.Name = "Wallpaper1"
$System_Drawing_SizeOwn = New-Object System.Drawing.Size
$System_Drawing_SizeOwn.Height = 49
$System_Drawing_SizeOwn.Width = 152
$buttonimgOwn.Size = $System_Drawing_SizeOwn
$buttonimgOwn.TabIndex = 0
$buttonimgOwn.Text = "Own Wallpaper"
$buttonimgOwn.UseVisualStyleBackColor = $True
$buttonimgOwn.add_Click($handler_buttonOwn_Click)

$pictureBoxOwn = new-object Windows.Forms.PictureBox
$pictureBoxOwn.Location = "350, 15"
$pictureBoxOwn.Height = 95
$pictureBoxOwn.Width = 152
$pictureBoxOwn.Image = $imgOwn;

$pictureBox1 = new-object Windows.Forms.PictureBox
$pictureBox1.Location = "15, 189"
$pictureBox1.Height = $img1.Size.Height
$pictureBox1.Width = $img1.Size.Width
$pictureBox1.Image = $img1

$System_Drawing_Point1 = New-Object System.Drawing.Point
$System_Drawing_Point1.X = 15
$System_Drawing_Point1.Y = 302
$buttonimg1.Location = $System_Drawing_Point1
$buttonimg1.Name = "Wallpaper1"
$System_Drawing_Size1 = New-Object System.Drawing.Size
$System_Drawing_Size1.Height = 49
$System_Drawing_Size1.Width = 152
$buttonimg1.Size = $System_Drawing_Size1
$buttonimg1.TabIndex = 0
$buttonimg1.Text = "Wallpaper Citrix"
$buttonimg1.UseVisualStyleBackColor = $True
$buttonimg1.add_Click($handler_button1_Click)

$pictureBox2 = new-object Windows.Forms.PictureBox
$pictureBox2.Location = "182, 189"
$pictureBox2.Height = $img2.Size.Height
$pictureBox2.Width = $img2.Size.Width
$pictureBox2.Image = $img2

$System_Drawing_Point2= New-Object System.Drawing.Point
$System_Drawing_Point2.X = 182
$System_Drawing_Point2.Y = 302
$buttonimg2.Location = $System_Drawing_Point2
$buttonimg2.Name = "Wallpaper2"
$System_Drawing_Size2 = New-Object System.Drawing.Size
$System_Drawing_Size2.Height = 49
$System_Drawing_Size2.Width = 152
$buttonimg2.Size = $System_Drawing_Size2
$buttonimg2.TabIndex = 0
$buttonimg2.Text = "Wallpaper fresh-it"
$buttonimg2.UseVisualStyleBackColor = $True
$buttonimg2.add_Click($handler_button2_Click)

$pictureBox3 = new-object Windows.Forms.PictureBox
$pictureBox3.Location = "350, 189"
$pictureBox3.Height = $img3.Size.Height
$pictureBox3.Width = $img3.Size.Width
$pictureBox3.Image = $img3

$System_Drawing_Point3= New-Object System.Drawing.Point
$System_Drawing_Point3.X = 350
$System_Drawing_Point3.Y = 302
$buttonimg3.Location = $System_Drawing_Point3
$buttonimg3.Name = "Wallpaper3"
$System_Drawing_Size3 = New-Object System.Drawing.Size
$System_Drawing_Size3.Height = 49
$System_Drawing_Size3.Width = 152
$buttonimg3.Size = $System_Drawing_Size3
$buttonimg3.TabIndex = 0
$buttonimg3.Text = "Wallpaper fresh-it-Logo"
$buttonimg3.UseVisualStyleBackColor = $True
$buttonimg3.add_Click($handler_button3_Click)

$labelFoot.Font = $FontFoot
$labelFoot.Text = "fresh-it.info (c) | support@fresh-it.info | Ver. 1.3/roe"
$labelFoot.Location = "15, 355"
$labelFoot.Height = 40
$labelFoot.Width = 250

$form1.Controls.Add($labelHead)
$form1.Controls.Add($label1)
$form1.Controls.Add($buttonupl)
$form1.Controls.Add($labelupl)
$form1.Controls.Add($buttonimgOwn)
$form1.Controls.Add($pictureBox1)
$form1.Controls.Add($pictureBox2)
$form1.Controls.Add($pictureBox3)
$form1.Controls.Add($pictureBoxOwn)
$form1.Controls.Add($buttonimg1)
$form1.Controls.Add($buttonimg2)
$form1.Controls.Add($buttonimg3)
$form1.Controls.Add($labelFoot)

#Save state of the form
$InitialFormWindowState = $form1.WindowState

#Correct OnLoad event to correct form state
$form1.add_Load($form1_StateCorrection_Load)

#Display defined form
return $form1.ShowDialog()
}

#Call function GenerateForm
if(OnApplicationLoad -eq $true)
{
GenerateForm | Out-Null
OnApplicationExit
}