About the Author: Michael Bogobowicz is a Principal Consultant with the World-Wide Consulting Solutions team, and has been engaged on Citrix XenApp and XenDesktop projects around the world since 2006.

XenApp 6 introduces some new and versatile PowerShell scripting – for those of you used to MFCOM scripting, you’ll find this to be a very welcome improvement. In this post, we’ll set up the XenApp 6 SDK and build a simple script that inventories the farm. To make things easy and allow us to get started quickly, we’ll install the SDK on a farm member and go from there. In a future post, we’ll explore remote PowerShell scripting. And speaking of future posts, I’m perfectly happy with you coming up with topics for me to write about – if there’s something bugging you about managing a XenApp environment, comment and let me know, and I’ll see if I can make a PowerShell script to help you out.

SDK Installation

Download the SDK from:  http://community.citrix.com/display/xa/XenApp+6+PowerShell+SDK. Since we’re installing this on a machine with XenApp 6 installed, all of the prereqs should already be there. Go ahead and unzip the package, and then run the installer.

If asked, update the execution policy to AllSigned. No other configuration changes should need to be made during the process.

Once the install process completes, go to Start -> All Programs -> Citrix -> XenApp Server SDK  and run Windows Powershell with Citrix (the second one is the 64-bit version):








The following prompt will appear requesting whether you “want to run software from this untrusted publisher?”  – type “A” to let the script always run. If you don’t, it may hang during scheduled runs in the future. And you’ll have lots of fun troubleshooting (trust makes everything easier!).








If all goes well, you should see the snap-ins loading as seen below:








Congratulations, you’ve now enabled the XenApp 6 SDK! Did I lose you yet? No? Good – this part will be slightly more difficult, but we’ll still keep it easy for today. Tomorrow – no promises…

Farm Inventory Script

Onto the scripting. As with any scripting language, you can go as lightweight as Notepad, or as heavy as Visual Studio. My preference is PowerGUI (www.powergui.org) for the combination of ease of use and power.

That being said, let’s start off with a basic script.

Step 1. Confirm PowerShell is Working

In your editor of choice, add the following line and execute the script:

echo "Starting farm check script..."






The “echo” command outputs the text in quotes, so you should see this text popup when you run the script. This command is a life-saver when debugging a script – use it liberally to indicate the position in the script and outputting the values of variables.

Step 2. Test that the XenApp Snap-ins are Loaded

Go ahead and run the following line:

 	Get-XAFarm






Output should include the Farm Name, Server Version, Administrator Type and the Session Count.

Step 3. Inventory the Environment

Let’s go ahead and pull information about our farm environment. For this script, we’ll just be getting a list of the published applications and servers in the farm – you’ll see that this can easily be customized to pull any of the application or server characteristics as well.

$farm = Get-XAFarm	#Gets the farm information
	$applications = Get-XAApplication	#Gets the published applications
	$servers = Get-XAServer	#Gets the XA servers in the farm


This sets the variables  that we’ll use to report on.

Step 4. Build the Report Output

We’ll want to format the information in a way that’s a easily readable, and weed out any application and server properties we’re not interested in. In this example, our final output will look something like this:






In order to accomplish this, the code might look something like this. Note that the `n indicates a newline (the ` character is the one above the ~ character on most Western keyboards), and that foreach creates a loop that runs through each object in the $servers and $applications arrays. Finally, the + symbol concatenates strings, and the += symbol means append to the existing information.

$output = "Farm: "+$farm.FarmName + "`n"
	$output += "`nServers `n"
	foreach($server in $servers){
		#This section shows information about the server name and the edition and version of XenApp installed
		$output+=$server.ServerName + " ("+$server.CitrixEdition+" "+$server.CitrixVersion+")`n"
	}

	$output += "`nApplications `n"
	foreach($application in $applications){
		#This section shows information on the application name and whether it's installed or streamed
		$output+=$application.DisplayName + " ("+$application.ApplicationType+")`n"
	}
	echo $output #This outputs all of the information we've added


The line “echo $output” will show the results of the inventory. Go ahead and run the script to make sure the information looks appropriate.

Step 5. Send an email report

Since there are a fair number of scripts that we’ll want to run on a regularly scheduled basis, we’ll often want email reports of what they find, or if the script encounters an issue that we need to be alerted about. In order to create an email to send,  for most Exchange configurations we’ll need to set up email in the following manner:

$msg = new-object Net.Mail.MailMessage
	$smtp = new-object Net.Mail.SmtpClient('smtp.citrite.net')     #Enter the SMTP Server as the argument

	$msg.From = 'robert.robertson@citrix.com'     #Set the From address here
	$msg.To.Add('robert.robertson@citrix.com'  )     #Add any recipients here
	$msg.Subject = "Farm Inventory" #The email subject can be anything
	$msg.Body = $output #Here we're putting the $output into the body of the email

	$smtp.Send($msg)

That’s it! If you want to download the full script, check it out at the XenApp Code Share or just get it from the direct link here.

**UPDATE** There is a new cmdlet that allows you to compete these actions in one step called SendMailMessage – additional information about it can be found at this TechNet article.

Step 6. Read more!

You made it through – congratulations! We have plenty of XenApp scripting coming up, so stick around on the Citrix blogs,  follow me on Twitter (@mcbogo), and sign up for next week’s TechTalk that will go over both XenApp and XenDesktop scripting.

How do you do that, you might be asking? Sign up here for the Essentials for using Windows PowerShell with XenApp and XenDesktop, set for Tuesday, August 24 from 2pm to 3pm EST. And if you’re interested in PowerShell scripting for XenDesktop, go check out Ed York’s XenDesktop blog series.

Oh, and finally, if you have any questions or requests for scripts, please comment! What’s really bugging you about managing a XenApp environment that we can help you fix?