PowerShell: Run a tool against Citrix Workergroup Servers

wg3

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

#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 }

view raw
delprof.ps1
hosted with ❤ by GitHub

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.

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

view raw
run-delprof2a.ps1
hosted with ❤ by GitHub

Then, I populated the list box with worker groups using get-xaworkergroup

$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)
}

view raw
run-delprof2b.ps1
hosted with ❤ by GitHub

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.

$wgObjForm.Controls.Add($objListBox)
$wgObjForm.Topmost = $True
$result = $wgObjForm.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
$workergroup = $objListBox.SelectedItem
} else {
exit 1
}

view raw
run-delprof2c.ps1
hosted with ❤ by GitHub

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.

# 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
}

view raw
run-delprof2d.ps1
hosted with ❤ by GitHub

Here’s the list box:

wg1

Selecting a Worker group and clicking OK, will run delprof2.exe against all the servers in the WG.

wg2

The script

You can get the script from Github.

Thanks for reading,
Alain Assaf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.