Intro
In my new job, I get to work on a dedicated Citrix team again and I’m really enjoying it. I get the opportunity to work collaboratively with a group of experienced Citrix Admins/Engineers and also get the chance to do a lot of PowerShell. Recently, we had to run Helge Klein’s excellent Delprof2 against a set of servers because of space issues. After fixing the issue, I thought it would be a good chance to stretch my PowerShell skills and enhance a tool the team uses.
Original Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region check for citrix API | |
if ( (Get-PSSnapin –Name Citrix.Common.Commands –ErrorAction SilentlyContinue) -eq $null ) | |
{ | |
Add-PsSnapin Citrix.Common.Commands | |
Add-PsSnapin Citrix.XenApp.Commands | |
Add-PsSnapin Citrix.Common.GroupPolicy | |
} | |
#endregion | |
$servers = (get-xaserver).servername | |
foreach ($server in $servers) | |
{c:\delprof2\delprof2.exe –c:$server /u } |
The original script runs fine, it just runs against the entire farm which in this case is over 700 servers. I wanted to create a script for our XenApp 6.5 environment and leverage Worker Groups to group our servers. I also wanted to try a graphical interface for the script.
PowerShell…GUI…what’s wrong with you?
I know, I know. Using a GUI with a PowerShell script is not typical, but I felt it was the best way to present a list of Worker Groups. Your Citrix environment may be smaller or not using that many worker groups, so displaying a list in the console may make more sense. I found this post which outlined how to do a list box in PowerShell.
First, I modified the dimensions of the parts of the list box so it would display all my worker groups.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type –AssemblyName System.Windows.Forms | |
Add-Type –AssemblyName System.Drawing | |
$wgObjForm = New-Object System.Windows.Forms.Form | |
$wgObjForm.Text = "Select a Worker Group" | |
$wgObjForm.Size = New-Object System.Drawing.Size(325,700) | |
$wgObjForm.StartPosition = "CenterScreen" | |
$OKButton = New-Object System.Windows.Forms.Button | |
$OKButton.Location = New-Object System.Drawing.Size(75,620) | |
$OKButton.Size = New-Object System.Drawing.Size(75,23) | |
$OKButton.Text = "OK" | |
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK | |
$wgObjForm.AcceptButton = $OKButton | |
$wgObjForm.Controls.Add($OKButton) | |
$CancelButton = New-Object System.Windows.Forms.Button | |
$CancelButton.Location = New-Object System.Drawing.Size(150,620) | |
$CancelButton.Size = New-Object System.Drawing.Size(75,23) | |
$CancelButton.Text = "Cancel" | |
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel | |
$wgObjForm.CancelButton = $CancelButton | |
$wgObjForm.Controls.Add($CancelButton) | |
$objLabel = New-Object System.Windows.Forms.Label | |
$objLabel.Location = New-Object System.Drawing.Point(10,15) | |
$objLabel.Size = New-Object System.Drawing.Size(280,30) | |
$objLabel.Text = "The computers in the workergroup will have delprof run against them:" | |
$wgObjForm.Controls.Add($objLabel) | |
$objListBox = New-Object System.Windows.Forms.ListBox | |
$objListBox.Location = New-Object System.Drawing.Point(10,45) | |
$objListBox.Size = New-Object System.Drawing.Size(275,50) | |
$objListBox.Height = 500 |
Then, I populated the list box with worker groups using get-xaworkergroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$workergroups = Get-XAWorkerGroup –ComputerName $xmlbroker | select WorkerGroupname | sort –Property workergroupname | |
# These 2 statements are far apart in the actual script, but together here for illustration | |
foreach ($wg in $workergroups) { | |
[void] $objListBox.Items.Add($wg.WorkerGroupName) | |
} |
Finally, I display the List box and wait for the user to select a Worker Group and click OK or Cancel and stop the script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$wgObjForm.Controls.Add($objListBox) | |
$wgObjForm.Topmost = $True | |
$result = $wgObjForm.ShowDialog() | |
if ($result -eq [System.Windows.Forms.DialogResult]::OK) { | |
$workergroup = $objListBox.SelectedItem | |
} else { | |
exit 1 | |
} |
If the user does pick a worker group and clicks OK, then we iterate through the servers in the Work Group and run delprof2.exe against them. This is where you could implement your own tool or procedure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get computers in workergroup | |
$wgservers = (Get-XAWorkerGroupServer –ComputerName $XMLBroker –WorkerGroupName $workergroup) | |
# Run DelProf against servers in workergroup | |
foreach ($server in $wgservers) { | |
$sname = $server.servername.Tostring() | |
$arg1 = '-c:' + $sname | |
$arg2 = '/u' | |
& $Delproflocation $arg1 $arg2 | |
} |
Here’s the list box:
Selecting a Worker group and clicking OK, will run delprof2.exe against all the servers in the WG.
The script
You can get the script from Github.
Thanks for reading,
Alain Assaf