Need script to pull citrix sessions based on AD server list

Need script to pull citrix sessions based on AD server list
sureshkumaritAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
{
    [cmdletbinding()]
    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [Alias('Name','IP','IPAddress')]
       [string]$Computer
    )

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

    if ($PingStatus.StatusCode -eq 0) {
        $true
    }
    else
    {
        $false
    }
}
 
$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
if($CreateTranscript)
{
$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;}
    </Style>'

$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>
        <TR>
                  <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>
        </TR>
        " 

ForEach ($ServerName in $ServerList.Name)
{
If (Get-PingStatus $ServerName)
    {
    Try{
    $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"}
    }
    Catch{
    }
    }
                $HTML += "<TR>
                              <TD>$($ServerName.ToString().ToUpper())</TD>
                              <TD>$(($TSSessions.State | WHERE {$_ -eq "Active"}).count)</TD>
                              <TD>$(($TSSessions.State | WHERE {$_ -like "*Disconnected"}).count)</TD>
                    <TD>$($BootTime.LastBootUpTime)</TD>
                    <TD bgcolor=`"$(if($IMAStatus.Status -eq "Stopped"){"F5A9A9"})`">$($IMAStatus.Status)</TD>
                    <TD bgcolor=`"$(if($XMLStatus.Status -eq "Stopped"){"F5A9A9"})`">$($XMLStatus.Status)</TD>
                        </TR>"
}
 
$HTML += "<H2></Table></BODY></HTML>"
$Header + $HTML | Out-File $OutputFile
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sureshkumaritAuthor Commented:
Tested and its working. Thanks for your help and response.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Citrix

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.