[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

Need script to pull citrix sessions based on AD server list

Need script to pull citrix sessions based on AD server list
1 Solution
Sekar ChinnakannuStaff EngineerCommented:
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
sureshkumaritAuthor Commented:
Tested and its working. Thanks for your help and response.

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now