Thank you to all those that attended the Essentials for using Windows PowerShell with XenApp and XenDesktop  Tech Talk on August 24th, 2010 – we had a fantastic turnout! For those of you that missed it, both the recording and presentation have been posted.

This FAQ focuses answering questions on the XenApp Section of the presentation, and has answers about functional questions as well as useful scripts. For the XenDesktop side, please see the XenDesktop PowerShell SDK FAQ. If you have any XenApp questions that weren’t answered, please post them here!

Here is the full list of questions answered in this FAQ:

  • Can you use the PowerShell SDK for XenApp 5 (2003 or 2008)?
  • How do you set up Remote Administration for a XenApp server?
  • Can PowerShell manage other products, such as Provisioning Services, XenServer, ESX, SQL?
  • Do the SDKs get installed separately from the main product?
  • Is there a book available for training on Citrix Powershell SDKs?
  • Is there an MFCOM to PowerShell converter?
  • How would I remove multiple XenApp servers from a specific published app?
  • How would I disable a specific app in a XenApp farm?
  • Is it possible to set Custom administrator above particular application folders?
  • How would you enable multiple applications to a single server?
  • How do I disable logons to a specific server?
  • Is there a way to query which is the highest currently utilized application and how much CPU per user it’s taking up?
  • Is there a way to check for disconnected or down sessions?
  • Can you monitor/interact with XenApp sessions?
  • Can you export all farm settings for backup?
  • Is the information on the SDK from the LHC (Local Host Cache) or the Data Store?
  • Are scripts to PowerShell encrypted when sent?
  • Are there plans for making SOAP/REST API available, or iPhone/Android?

XenApp FAQ

Q: Can you use the PowerShell SDK for XenApp 5 (2003 or 2008)?

A: Yes, you can (with caveats!). By installing the XenApp Commands Tech Preview v2 (login required), you’ll have access to almost identical commands as available in XenApp 6. That being said, not all commandlets will work exactly the same, though they will be close. All scripts so far written on this blog work the same on XenApp 5 for 2003 as they do on XenApp 6 for 2008 R2.

That being said, the XenApp 5 commandlets are supposed to be more of a stepping stone for XenApp 6. Support is provided for XenApp 5 scripts via the Developer Forums. So go at it and have fun!



Q: How do you set up Remote Administration for a XenApp server?

A: Remote administration is possible and can be set up in several ways, depending on whether Citrix administrators are also local administrators or not. There are also considerations around limitations. Please see an upcoming blog post for additional information about Remoting, or see the SDK help file under “Remoting,” available after the XenApp 6 SDK is installed.

UPDATE: Tom Kludy has published a couple of great posts on how to set up remoting. See his blogs on setting up XenApp 6 for remote scripting and remote programmingwith the PowerShell SDK.

Q: Can PowerShell manage other products, such as Provisioning Services, XenServer, ESX, SQL?

A: Yes, as long as these products have PowerShell interfaces, you can manage them via PowerShell.



Q: Can the PowerShell SDKs be used to check Citrix Policies?

A: Yes, the PowerShell SDK can be used to check and configure Citrix policies. A future blog will look at how to do it.



Q: Do the SDKs get installed separately from the main product?

A: Yes, the commandlets need to be installed separately. You can download the SDKs fromdownload.citrix.com.  Go to the XenApp product category, and scroll down until you see “XenApp SDKs” – the XenApp 5 & 6 PowerShell SDKs will be at the bottom of that list.



Q: Is there a book available for training on Citrix Powershell SDKs?

A: There aren’t any books out (yet), though there is a lot of information out there. There are a lot of resources out there already for XenApp:

Q: Is there an MFCOM to PowerShell converter?

A: As the underlying code base is completely different, there is no direct converter. However, there is a tool that can assist with mapping MFCOM to PowerShell functions (CTX12508)

Q: How would I remove multiple XenApp servers from a specific published app?

A: There are a few ways to do this:

#This will remove specific servers from the application
Remove-XAApplicationServer -BrowserName Calculator -ServerNames <span class="code-quote">"CCSXA601"</span>, <span class="code-quote">"CCSXA602"</span>

#This will remove all servers from the application
Remove-XAApplicationServer -BrowserName Calculator -ServerNames *
#This will set only the servers listed as providing the applications?

Set-XAApplication -BrowserName Calculator -ServerNames <span class="code-quote">"CCSXA601"</span>, <span class="code-quote">"CCSXA602"</span>


Q: How would I disable a specific app in a XenApp farm?








A: The following code will answer that question – please note that you also want to hide the application when you disable it, so that users don’t continue to see the icon.

Set-XAApplication -BrowserName Calculator -Enabled $<span class="code-keyword">false</span> -HideWhenDisabled $<span class="code-keyword">true</span>


Q: Is it possible to set Custom administrator above particular application folders?








A: Yes, with the following code. Note that it is recommended to add the domain in the @DOMAIN format to the username, the same as in the XenApp console.

Add-XAAdministratorPrivilege -AdministratorName <span class="code-quote">"edy@CITRITE"</span> -FolderPath <span class="code-quote">"Applications/New Admin Folder/AnotherTest"</span> -FolderPrivileges LogOffSessions, ViewSessions, ResetSessions, SendMessages

The code above assumes that the administrator already exists in the database, and you’re just adding permissions. We can make the code more intelligent to see if the administrator exists and create him if he doesn’t exist:

<span class="code-keyword">if</span>((Get-XAAdministrator -AdministratorName <span class="code-quote">"edyf@CITRITE"</span>) -eq $<span class="code-keyword">null</span>)
{
    New-XAAdministrator \-AdministratorName <span class="code-quote">"edy@CITRITE"</span> \-AdministratorType Custom
}
Add-XAAdministratorPrivilege -AdministratorName <span class="code-quote">"edy@CITRITE"</span> -FolderPath <span class="code-quote">"Applications/New Admin Folder/AnotherTest"</span> -FolderPrivileges LogOffSessions, ViewSessions, ResetSessions, SendMessages

Q: How would you enable multiple applications to a single server?

A: If you’re adding new applications that does exist in the farm:

New-XAApplication -BrowserName <span class="code-quote">"Temporary Application 2"</span> -DisplayName <span class="code-quote">"Temporary Application 2"</span> -Accounts <span class="code-quote">"Domain Users@CITRITE"</span> -CommandLineExecutable <span class="code-quote">"C:\Windows\system32\gpedit.msc"</span> -ApplicationType <span class="code-quote">"ServerInstalled"</span> -ServerNames <span class="code-quote">"CCSXA601"</span>

If you’re adding existing applications to a server:

Add-XAApplicationServer -BrowserName <span class="code-quote">"Temporary Application"</span> -ServerNames <span class="code-quote">"CCSXA602"</span>


Q: How do I disable logons to a specific server?








A: See the following code:

Disable-XAServerLogOn -ServerName <span class="code-quote">"CCSXA601"</span>

And to renable:

Enable-XAServerLogOn -ServerName <span class="code-quote">"CCSXA601"</span>


Q: Is there a way to query which is the highest currently utilized application and how much CPU per user it’s taking up?










A: Sure, there are lots of ways to do it – for the example below, we’re going to assume our goal is to find out which application is using the most total CPU (sum of the user CPUs), and then output a list of the users with their CPU usage on one server.

$apps=Get-XASessionProcess CCSXA601
$cpus = @{}
foreach($app in $apps){
  <span class="code-keyword">if</span>($app.AccountDisplayName -ne $<span class="code-keyword">null</span> -and $app.AccountDisplayName -ne ""){
    Write-Host $app.AccountDisplayName $app.ProcessName  $app.PercentCpuLoad  $app.CurrentWorkingSetSize
    <span class="code-keyword">if</span>($cpus.get_Item($app.ProcessName)-eq $<span class="code-keyword">null</span>){
      $cpus.Add($app.ProcessName, $app.PercentCpuLoad)
      echo <span class="code-quote">"Adding $app to our hash table."</span>
    } <span class="code-keyword">else</span> {
      echo <span class="code-quote">"$app already exists. Adding CPU percentages."</span>
      $cpus.set_Item($app.ProcessName, $cpus.get_Item($app.ProcessName)+$app.PercentCpuLoad);
    }

  }
}

$cpus_rank=$cpus.GetEnumerator() | Sort-<span class="code-object">Object</span> Value -Descending #This creates an ordered list
$cpu_high=$cpus_rank[0] #<span class="code-keyword">this</span> creates an object with the highest cpu value

echo <span class="code-quote">"**********"</span>
echo <span class="code-quote">"The most active process is: "</span> $cpu_high.get_Key()
echo <span class="code-quote">"**********"</span>
echo <span class="code-quote">"Used by: "</span>

foreach($app in $apps){
  <span class="code-keyword">if</span>($app.AccountDisplayName -ne $<span class="code-keyword">null</span> -and $app.AccountDisplayName -ne "" -and $app.ProcessName -eq $cpu_high.get_Key()){
    Write-Host $app.AccountDisplayName $app.ProcessName  $app.PercentCpuLoad  $app.CurrentWorkingSetSize
  }
}


Q: Is there a way to check for disconnected or down sessions?








A: Absolutely – see the following code. It can be modified to wait and check in 10 seconds to see if a session has been cleared, and if not, clear it.

$sessions = Get-XASession
foreach($session in $sessions){
    <span class="code-keyword">if</span>($session.Protocol -eq <span class="code-quote">"Ica"</span> -and $session.State -ne <span class="code-quote">"Listening"</span> -and $session.State -ne <span class="code-quote">"Active"</span>)
    {
        Write-Host $session.SessionName $session.ServerName $session.AccountName $session.BrowserName $session.State $session.Protocol
    }
}


Q: Can you monitor/interact with XenApp sessions?








A: Yes – several scripts here and in my blog have shown how you can monitor. Interaction is possible, though it depends largely on non-Citrix PowerShell commands, and may require some customization depending on what’s needed.

Q: Can you export all farm settings for backup?

A: Yes.



Q: Is the information on the SDK from the LHC (Local Host Cache) or the Data Store?

A: Both MFCOM and PowerShell interact only with the Data Store. If you’re constantly checking the local MMC console to confirm changes have been made, you may see inconsistencies as a result, because that information may be coming from the LHC.

Q: Are scripts to PowerShell encrypted when sent?

A: If we’re referring to the remote module, then you’ll be happy to know that encryption is an option. The PowerShell channel is an implement of the WS-Man (uses WinRM), in which you can specify different authentication options. See the help for Invoke-Command for more details.

Q: Are there plans for making SOAP/REST API available, or iPhone/Android?

A: Additional API plans are in the works.