XenApp and XenDesktop 7 Useful Powershell Reference

Here’s a quick list of Powershell commands I find myself using frequently. I’ll keep this updated.

Get a list of all brokered connections
[code language=”powershell”]
Get-BrokerConnectionLog | Sort BrokeringTime
[/code]

I always sort by BrokeringTime because the order appears to be odd otherwise because the log is updated with the initial brokering time as well as the EndTime.

Add an application to multiple delivery groups

[code language=”powershell”]
Add-BrokerApplication -Name "AppNameHere" -DesktopGroup "NewDesktopGroupHere"
[/code]

Adding an application to multiple desktop groups is 100% supported, it’s just not exposed in the GUI as of XenApp 7.5. Otherwise, if you try to add an application that already exists, you’ll get “Microsoft Word_1″. Also note when you do this, you can also set a priority to different desktop group- by default, the priority is added as zero – take a look:

[code language=”powershell”]
PS C:\Windows\system32> Get-BrokerApplication -Name "Notepad"
ApplicationType : HostedOnDesktop
AssociatedDesktopGroupPriorities : {0, 0}
AssociatedDesktopGroupUids : {1, 2}
AssociatedUserFullNames : {Domain Users, Jake Rutski}
AssociatedUserNames : {lab\Domain Users, lab\jrutski}
AssociatedUserUPNs : {, [email protected]}
BrowserName : Notepad
ClientFolder : AppCategory
CommandLineArguments :
[/code]

To set a different priority, use the ‘-priority’ argument:

[code language=”powershell”]
Add-BrokerApplication -name "notepad" -DesktopGroup "Test Delivery Group" -Priority 5
[/code]

View the SQL DB info for the site

[code language=”powershell”]
Get-BrokerDBConnection
Server=TESTSQL1;Initial Catalog=CitrixTestSite;Integrated Security=True
[/code]

Monitoring Scripts

This script was used with a monitoring solution to return the count of unregistered machines, not in maintenance mode, powered on, and with a user assigned to the desktop:

[code language=”powershell”]
(Get-BrokerDesktop -MaxRecordCount 2000 -AdminAddress yourddc.goes.here | Where {($_.RegistrationState -eq "Unregistered") -and ($_.AssociatedUserNames) -and !($_.InMaintenanceMode) -and ($_.PowerState -eq "On")}).Count
[/code]

This script returns the count of session disconnections for the last 5 minutes – note that the polling interval for this script would be every 5 minutes. If you wanted to poll every 1 minute for example, change to AddMinutes(-1):

[code language=”powershell”]
(Get-BrokerSession -SessionState Disconnected -AdminAddress yourddc.goes.here | Where {$_.SessionStateChangeTime -gt ((Get-Date).AddMinutes(-5))}).Count
[/code]

Static Machine Addition
 
This script will get the ID of a VM, add it to the machine catalog with the CatalogUid of 3 and HypervisorConnectionUid of 2 (change these to match your needs); then assign a user and add to a catalog. Note a few variables used: $NewCluster and $NewVMName are specific to the path to the VM object; $NewVMName is the computer name of the machine.

[code language=”powershell”]
$NewVMObj = Get-ADComputer -Server your.domain.controller -Filter {Name -eq $NewVMName}
Set-HypAdminConnection  -AdminAddress "yourddc.goes.here"
$XDVMId = (Get-Item "XDHyp:\Connections\Your Cluster\Your.datacenter\$($NewCluster).cluster\$($NewVMName).vm").Id
# Add machine to machine catalog; add user to machine
New-BrokerMachine -AdminAddress "yourddc.goes.here" -CatalogUid 3 -HostedMachineId $XDVMId -HypervisorConnectionUid 2 -MachineName $NewVMObj.SID
Add-BrokerUser  -AdminAddress "yourddc.goes.here" -Machine (Get-BrokerMachine -SID $NewVMObj.SID).Uid -Name "YOURDOMAIN\$($NewUser)"
# Add machine to desktop group;
Get-BrokerMachine -SID $NewVMObj.SID | Add-BrokerMachine  -AdminAddress "yourddc.goes.here" -DesktopGroup "Your Desktop Group Here"
[/code]

XenDesktop Machines that have not been logged in to in the past XX days
 
This is for statically assigned VMs or PvD XD VMs, but it outputs nice human readable user names – requires AD PowerShell Snapin.

[code language=”powershell”]
Get-BrokerDesktop -MaxRecordCount 2000 | Where {((Get-Date) – $_.LastConnectionTime).Days -gt 60} | sort LastConnectionTime | Select DNSName,LastConnectionTime,@{Name="UserName";Expression={$UserN = $_.AssociatedUserNames.Replace("YOURDOMAINHERE\",""); $UserObj = Get-ADUser -Filter {Name -eq $UserN}; "$($UserObj.GivenName) $($UserObj.Surname)"}}
[/code]

3 thoughts on “XenApp and XenDesktop 7 Useful Powershell Reference”

  1. hi,

    could you please help me in the below two things:
    1-could you please provide me the command line using power shell to get the below requirements.
    1-i want get the list of all delivery group defined within the DDC.
    2-i want to extract the list of delivery group in which the Mozilla Firefox is installed.

    Reply
    • Prangya,

      1. Get-BrokerDesktopGroup -Property Name,DeliveryType,DesktopKind

      Use the -Property parameter so that you don’t get the entire object returned.

      2. Get-BrokerDesktopGroup | Where {$_.UUID -in ((Get-BrokerApplication -Name ‘Mozilla Firefox’).AssociatedDesktopGroupUUIDs.GUID)} | Select Name,DeliveryType,DesktopKind

      Again using the Select so that you don’t get the entire desktopGroup object

      Reply
  2. Hi,
    Could you help me in below situation
    I have application google chrome which is published from shared application citrix server with controller 7.12 and vda 7.15 ,I want below
    1. Disconnect the idle session which is more than an hours idle. for the application Google Chrome
    2 Than log off that disconnected session which is using google chrome but i only want that session not all other sessions which are opened from that user.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.