Creating a sample C#.NET application

Design and implement Citrix farms based on XenApp 6.5 with this book and ebook

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\ 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);


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:

Books to Consider

comments powered by Disqus