Creating a sample C#.NET application

Exclusive offer: get 50% off this eBook here
Getting Started with Citrix XenApp 6.5

Getting Started with Citrix XenApp 6.5 — Save 50%

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

$35.99    $18.00
by Guillermo Musumeci | July 2012 | .NET

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:


Getting Started with Citrix XenApp 6.5 Design and implement Citrix farms based on XenApp 6.5 with this book and ebook
Published: July 2012
eBook Price: $35.99
Book Price: $59.99
See more
Select your format and quantity:

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


Getting Started with Citrix XenApp 6
Getting Started with Citrix XenApp 6

Microsoft SQL Server 2008 R2 Administration Cookbook
Microsoft SQL Server 2008 R2 Administration Cookbook

Citrix XenServer 6.0 Administration Essential Guide
Citrix XenServer 6.0 Administration Essential Guide

Microsoft Forefront UAG 2010 Administrator's Handbook
Microsoft Forefront UAG 2010 Administrator's Handbook

Getting Started with Microsoft Application Virtualization 4.6
Getting Started with Microsoft Application Virtualization 4.6

Microsoft Dynamics AX 2009 Administration
Microsoft Dynamics AX 2009 Administration

Getting Started With Oracle SOA Suite 11g R1 – A Hands-On Tutorial
Getting Started With Oracle SOA Suite 11g R1 – A Hands-On Tutorial

IBM WebSphere Application Server 8.0 Administration Guide
IBM WebSphere Application Server 8.0 Administration Guide


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
T
Y
i
X
f
h
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software