Need script to pull citrix sessions based on AD server list

Posted on 2014-08-27
Last Modified: 2014-08-27
Need script to pull citrix sessions based on AD server list
Question by:sureshkumarit
    LVL 24

    Accepted Solution

    Yes you can using the below script, make sure you run in proper domain to pull the list of citrix servers and session details

    function Get-PingStatus

        $PingStatus = Get-WmiObject -Query "SELECT StatusCode FROM win32_PingStatus WHERE ADDRESS = '$Computer'"

        if ($PingStatus.StatusCode -eq 0) {
    $Filter = 'Name -like "*SERVERCITRIX*" -OR Name -like "*SERVERCTX*" -AND Name -notlike "*TST*" -AND Name -notlike "*DEV*"'
    $ServerList = Get-ADComputer -Properties Name -Filter $Filter | Select Name | Sort Name
    $OutputFile = "C:\Users\user\Desktop\ss.html"
    $DomainName = (Get-ADDomain).NetBIOSName  
    $Time = Get-Date -Format t
    $CurrDate = Get-Date -UFormat "%D"
    $CreateTranscript = $false
    $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
    if( -not (Test-Path ($scriptDir + "\Transcripts"))){New-Item -ItemType Directory -Path ($scriptDir + "\Transcripts")}
    Start-Transcript -Path ($scriptDir + "\Transcripts\{0:yyyyMMdd}_Log.txt"  -f $(get-date)) -Append

    Import-Module ActiveDirectory
    Import-Module PSTerminalServices

    $HTML = '<style type="text/css">
    #TSHead body {font: normal small sans-serif;}
    #TSHead table {border-collapse: collapse; width: 100%; background-color:#F5F5F5;}
    #TSHead th {font: normal small sans-serif;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#7FB1B3;}
    #TSHead th, td {font: normal small sans-serif; padding: 0.25rem;text-align: left;border: 1px solid #FFFFFF;}
    #TSHead tbody tr:nth-child(odd) {background: #D3D3D3;}

    $Header = "<H2 align=center><font face=Arial>$DomainName Citrix Sessions as of $Time on $CurrDate</font></H2>"  

    $HTML += "<HTML><BODY><script src=sorttable.js></script><Table border=1 cellpadding=0 cellspacing=0 width=100% id=TSHead class=sortable>
                      <TH><B>Citrix Server</B></TH>
                      <TH><B>Active ICA Sessions</B></TH>
                      <TH><B>Disconnected Sessions</B></TH>
                      <TH><B>Last Boot Time</B></TH>
                      <TH><B>Citrix IMA</B></TH>
                      <TH><B>Citrix XML</B></TH>

    ForEach ($ServerName in $ServerList.Name)
    If (Get-PingStatus $ServerName)
        $TSSessions = Get-TSSession -ComputerName $ServerName -ErrorAction SilentlyContinue | Select UserAccount,State,WindowStationName | WHERE {$_.UserAccount -ne $null -AND $_.WindowStationName -notlike "*RDP*" -AND $_.WindowStationName -notlike "*Services*" -AND $_.WindowStationName -notlike "*Console*"}
        $BootTime = Get-WmiObject -ComputerName $ServerName win32_operatingsystem | select @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.LastBootUpTime)}}
        $IMAStatus = get-service -ComputerName $ServerName | WHERE {$_.Name -eq "IMAService"}
        $XMLStatus = get-service -ComputerName $ServerName | WHERE {$_.Name -eq "CtxHttp"}
                    $HTML += "<TR>
                                  <TD>$(($TSSessions.State | WHERE {$_ -eq "Active"}).count)</TD>
                                  <TD>$(($TSSessions.State | WHERE {$_ -like "*Disconnected"}).count)</TD>
                        <TD bgcolor=`"$(if($IMAStatus.Status -eq "Stopped"){"F5A9A9"})`">$($IMAStatus.Status)</TD>
                        <TD bgcolor=`"$(if($XMLStatus.Status -eq "Stopped"){"F5A9A9"})`">$($XMLStatus.Status)</TD>
    $HTML += "<H2></Table></BODY></HTML>"
    $Header + $HTML | Out-File $OutputFile

    Author Comment

    Tested and its working. Thanks for your help and response.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    #Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
    Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
    This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
    This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now