Finding Offline Citrix XenApp Servers Using Microsoft PowerShell Part 4 of 4

May 21, 2011

PowerShell, XenApp

In Part 3 of this 4 Part series, the PowerShell script from Parts 1 and 2 was modified to list all the offline Citrix XenApp servers in a XenApp farm by Zone and validate the Zone name entered.  The original question was could PowerShell be used to get a list of offline servers.  The answer is yes.  But, there is another way.

At The Experts Conference in June 2011, I took a PowerShell class given by Don Jones.  One of the questions he asked was “how would you map a drive letter using PowerShell?”  The answer?

Net use d: \\Server\Share

How do you find offline XenApp servers using PowerShell?

Qfarm /offline

(Figure 1)

Figure 1

Being fairly new to PowerShell, I thought I could just run

qfarm /offline | convertto-html

and my job would be done.  But it was not that easy.  Fortunately, my friend and Exchange MVP Michael B. Smith is also a PowerShell guru and helped me walk through the learning process.  What follows is how Michael helped me generate HTML output.

What did I get from running the commands?

qfarm /offline | convertto-html

Garbage! (Figure 2)

Figure 2

The first thing Michael had me do was determine the type of output being generated by running:

$obj = qfarm /offline

This assigns the results of qfarm /offline to a variable $obj.  Then the “type” of the variable is retrieved (Figure 3).

Figure 3

Next, find the type of the members of $obj (Figure 4) by using

$obj | get-member

Figure 4

Since $obj is an array, Michael also wanted the members of the first element of the $obj array (Figure 5).

$obj[0] | get-member

Note: PowerShell arrays start at 0.

Figure 5

Michael then had me run the following code (results shown in Figure 6):

for( $i=0; $i -lt $obj.Count; $i++ ) { $obj[$i].Gettype().fullname; $obj[$i].Length }

Figure 6

Does the output look familiar?  Compare Figure 6 to Figure 2.  Michael then showed me how to get formatted HTML output similar to the qfarm /offline output (Figure 7).

$str = “”
$obj = qfarm /offline
for( $i=0 ; $i --lt $obj.Count ; $i++ ) { $str += $obj[ $i ] + “<br />”; }
ConvertTo-HTML --body $str > C:\test.html
Invoke-Item c:\test.html
$str = $null
$obj = $null

Figure 7

If you know how to create and use a CSS file, the ConvertTo-HTML cmdlet allows one to be used to format the output more precisely.

With PowerShell, there are usually several ways to accomplish a given task.  If there is a command line utility available, use it.  There is no need to reinvent the wheel.

, ,

About Carl Webster

Webster is a Sr. Solutions Architect for Choice Solutions, LLC and specializes in Citrix, Active Directory and Technical Documentation. Webster has been working with Citrix products for many years starting with Multi-User OS/2 in 1990.

View all posts by Carl Webster

No comments yet.

Leave a Reply