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.
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…
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.
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.
Go ahead and run the following line:
Output should include the Farm Name, Server Version, Administrator Type and the Session Count.
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.
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.
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:
**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.
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?