Building a XenDesktop PowerShell Web App
In our environment we have internal chargebacks and a signup process that automates granting of access to all our internal Citrix product offerings (until now, done with Active Directory group memberships). With XenDesktop, PowerShell was made the ONLY automation interface into the Desktop Delivery Controller.
Getting a PowerShell-Based XenDesktop application to function from a Visual Studio-Based web appliction was quite tricky because the Citrix developers of the XdCommands snapin did not provide any ability to add a credential object when loading the snapin or when using the cmdlet New-XdAdminConnection. When running as a console app, credentials in Windows are automatically passed-through, but not from a web application.
So for those who need to do it, here's how to make it work:
Aside from getting basic Windows PowerShell to work from a web app, you will need to have a dedicated IIS application pool for the app which you will then to configure with a domain account that has rights to control the desktop delivery controller(s) you will be working with. 1. Have a separate application pool for the web app that runs the xdcommands powershell pieces
2. Change that application pool's Identity from (usually) Network Service to a domain account that has rights to perform the tasks in the farms that the DDC(s) operate in. This is best done with a 'service account' that is not set to have it's password changing.
3. It is easiest if this account is also be a member of the local administrator's group on the web server. 4. The account must also have 'logon as a batch job' windows right. The 'XdCommands' will now pass through the credentials to the DDC when run from a web server codebehind page.
Here's a snippet of actual working VB code (C# code is identical, just syntactically different):
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Label1.Text = "Current XenDesktop Sessions"
Label1.Visible = True
Dim myRunSpace As Runspace
Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
Dim snapInException As New PSSnapInException
Dim info As PSSnapInInfo
info = rsConfig.AddPSSnapIn("XdCommands", snapInException)
myRunSpace = RunspaceFactory.CreateRunspace(rsConfig)
myRunSpace.Open()
Dim enumXdSessions As New StringBuilder
enumXdSessions.AppendLine("$farm = New-XdAdminConnection '10.X.X.X'")
enumXdSessions.AppendLine("get-xdsession")
Dim pipe As Pipeline = myRunSpace.CreatePipeline()
pipe.Commands.AddScript(enumXdSessions.ToString)
Dim resultObject As System.Collections.ObjectModel.Collection(Of System.Management.Automation.PSObject)
Err.Clear()
resultObject = pipe.Invoke()
If Not Err.Number = 0 Then
Response.Write(Err.Number.ToString & " " & Err.Description)
End If
myRunSpace.Close()
TableHandler.CreateHeader(Table1, "XdSession")
For Each obj As PSObject In resultObject
TableHandler.AddDataRow(Table1, "XdSession", obj, "", "", "", "")
Next
End Sub
The 'TableHandler' in my project here is just a class that handles adding the data in 'obj' to a table on the page for a nice display, so don't worry about that. But using Visual Studio, 'obj' will contain all the object info about each session found on the DDC. Add a 'watch' for obj and you will see all the available information for all of the sessions currently existing on your DDC.
To make a Web-App Desktop Delivery Controller PowerShell project work you need:
1. XenDesktopControllerSDK.msi installed on all production and development web servers (obtained from Citrix)
2. .Net Framework 3.5 or higher installed on all production and development web servers (obtained from Microsoft)
3. Windows Powershell KB926139 (obtained from microsoft- there is one for each particular OS version)
4. Windows Powershell SDK (only needed on development server). The Powershell assemblies are part of the 'Windows SDK for Windows 2008 and .Net Framework 3.5'. Don't worry about the OS version it will install on any OS greater than Windows 2000. All that is needed is the '.Net Development Tools' option in the install screen5. Add a reference to the 'system.management.automation.dll' and might want to copy it to the 'bin' directory of your web app so that it gets pushed to the production web server.
6. Add Imports System.Management.Automation
Imports System.Management.Automation.Host
Imports System.Management.Automation.Runspaces
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Runtime.InteropServices
Imports System.Text
Or the C# equivalents if you're coding C#
Add Comment