Citrix: Getting a Farm Inventory With XenApp 6 PowerShell Scripting

I found the following useful information on the Citrix blogs written by Michael Bogobowicz

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:





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:





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.

 

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.

 

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:

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.

 

It’s an excellent way to get started in an unknown Citrix XenApp 6 farm with xenapp 6 powershell scripts, the output it creates can be used when inventorying a new environment.

 

The following two tabs change content below.

Kees Baggerman

Kees Baggerman is a Staff Solutions Architect for End User Computing at Nutanix. Kees has driven numerous Microsoft and Citrix, and RES infrastructures functional/technical designs, migrations, implementations engagements over the years.

Leave a Reply

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