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

x
  • 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
0
sureshkumarit
Asked:
sureshkumarit
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
{
    [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
 
sureshkumaritAuthor Commented:
Tested and its working. Thanks for your help and response.
0

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