Your message has been sent.
This article has been saved to your account.
Go to my account
This article has been emailed to your Kindle.
Send this article
Complete the form below to send this article, Creating a sample C#.NET application, to a friend (or to yourself). We will never share your details (or your friend's) with anyone. For more information, read our Privacy Policy.
Citrix XenApp is the leader of application virtualization or application delivery. Citrix was founded in 1989 and they developed the first successful product in 1993 called WinView. It provided remote access to DOS and Windows 3.1 applications on a multi-user platform. Citrix licensed Microsoft's Windows NT 3.51 source code from Microsoft; and in 1995, they shipped a multiuser version of Windows NT based on the MultiWin engine, known as WinFrame. This allowed multiple users to log on and execute applications on a WinFrame server. In 1996, Citrix licensed the MultiWin technology to Microsoft, establishing the foundation of Microsoft's Terminal Services.
This article by Guillermo Musumeci, the author of Getting Started with Citrix XenApp 6.5, will create a sample application to list all XenApp servers in our farm using Citrix XenApp Commands with managed code. Samples contained in this article are developed in Microsoft C# 2010 Express Edition.
First, open C#.NET. Then, go to File | New Project | Windows Form Applications. Type the desired name for our project and click on the OK button.

(Move the mouse over the image to enlarge.)
Adding references
We need to add a reference to the System.Management.Automation.dll assembly. Adding this assembly is tricky; first, we need to copy the assembly file to our application folder using the following command:
Copy %windir%\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31b f3856ad364e35\System.Management.Automation.dll C:\Code\XA65Sample
where C:\Code\XA65Sample is the folder of our application.
Then we need to add the reference to the assembly. In the Project menu, we need to select Add Reference, click on the Browse tab, search and select the file System. Management.Automation.dll.

After referencing the assembly, we need to add the following directive statements to our code:
using System.Management.Automation; using System.Management.Automation.Host; using System.Management.Automation.Runspaces;
Also, adding the following directive statements will make it easier to work with the collections returned from the commands:
using System.Collections.Generic; using System.Collections.ObjectModel;
Creating and opening a runspace
To use the Microsoft Windows PowerShell and Citrix XenApp Commands from managed code, we must first create and open a runspace. A runspace provides a way for the application to execute pipelines programmatically. Runspaces construct a logical model of execution using pipelines that contains cmdlets, native commands, and language elements.
So let's go and create a new function called ShowXAServers for the new runspace:
void ShowXAServers()
Then the following code creates a new instance of a runspace and opens it:
Runspace myRunspace = RunspaceFactory.CreateRunspace(); myRunspace.Open();
The preceding piece of code provides access only to the cmdlets that come with the default Windows PowerShell installation. To use the cmdlets included with XenApp Commands, we must call it using an instance of the RunspaceConfiguration class. The following code creates a runspace that has access to the XenApp Commands:
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); PSSnapInException snapInException=null; PSSnapInInfo info = rsConfig.AddPSSnapIn ("Citrix.XenApp.Commands", out snapInException); Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig); myRunSpace.Open();
This code specifies that we want to use Windows PowerShell in the XenApp Command context. This step gives us access to Windows PowerShell cmdlets and Citrix-specific cmdlets.
Running a cmdlet
Next, we need to create an instance of the Command class by using the name of the cmdlet that we want to run. The following code creates an instance of the Command class that will run the Get-XAServer cmdlet, add the command to the Commands collection of the pipeline, and finally run the command calling the Pipeline.Invoke method:
Pipeline pipeLine = myRunSpace.CreatePipeline(); Command myCommand = newCommand("Get-XAServer"); pipeLine.Commands.Add(myCommand); Collection
commandResults = pipeLine.Invoke();
Displaying results
Now we run the command Get-XAServer on the shell and get this output:

In the left-hand side column, the properties of the cmdlet are located, and in this case, we are looking for the first one, the ServerName, so we are going to redirect the output of the ServerName property to a ListBox.
So the next step will be to add a ListBox and Button controls. The ListBox will show the list of XenApp servers when we click the button.

Then we need to add the following code at the end of Function ShowXAServers:
foreach (PSObject cmdlet in commandResults) { string cmdletName = cmdlet.Properties["ServerName"].Value. ToString(); listBox1.Items.Add (cmdletName); }
The full code of the sample will look like this:

And this is the final output of the application when we run it:

Passing parameters to cmdlets
We can pass parameters to cmdlets, using the Parameters.Add option. We can add multiple parameters. Each parameter will require a line. For example, we can add the ZoneName parameter to filter server members of the US-Zone zone:
Command myCommand = newCommand("Get-XAServer"); myCommand.Parameters.Add("ZoneName", "US-ZONE") pipeLine.Commands.Add(myCommand);
Summary
In this article, we have learned about managing XenApp with Windows PowerShell and developed sample .NET applications on C#.NET. Specially, we saw:
- How to list all XenApp servers by using Citrix XenApp Commands
- How to add a reference to the System.Management.Automation.dll assembly
- How to create and open runspace, which helps to execute pipelines programmatically
- How to create an instance using the name of cmdlet
- How to pass parameters to cmdlets
Further resources on this subject:
- Designing a XenApp 6 Farm [Article]
- Getting Started with XenApp 6 [Article]
- Microsoft Forefront UAG Building Blocks [Article]
About the Author :
Guillermo Musumeci
Guillermo Musumeci is a Windows Infrastructure Architect specialized in Citrix and virtualization with 16 years of experience. He has a passion for designing, building, deploying, and supporting enterprise architectures using Citrix, Microsoft, and VMware products.
He worked as Project Manager and Senior Consultant in medium to large Citrix and virtualization projects in America, Europe, and recently he relocated to Asia, where he lives with his wife and two children.
Guillermo is also the founder and developer of the popular site CtxAdmTools, which provides free tools to manage Citrix environments, Active Directory, and more.
He holds more than 25 Citrix, Microsoft, and VMware certifications.
Books From Packt
|
|
|



Post new comment