Public/Get-CitrixSystemStatus.ps1
<#
.SYNOPSIS This PowerShell cmdlet fetches the count of Citrix machines based on various status filters. .DESCRIPTION Get-CitrixSystemStatus is a cmdlet that connects to a specified Citrix admin address and retrieves information about the machines based on their system status. The cmdlet can provide counts for different categories such as total desktops, total servers, registered or unregistered desktops and apps, and those in maintenance mode. The cmdlet uses various switches to filter the types of machines counted. .Syntax Get-CitrixSystemStatus -AdminAddress <string> [-TotalDesktops] [-TotalServers] [-RegisteredDesktops] [-UnRegisteredDesktops] [-RegisteredApps] [-UnRegisteredApps] [-GhostSessions] [-GhostConnectedSessions] [-AppMaintenanceMode] [-DesktopMaintenanceMode] [-CitrixRPA] [<CommonParameters>] .PARAMETER AdminAddress The address of the Citrix admin server from which the machine status is fetched. This parameter supports pipeline input. .PARAMETER TotalDesktops This switch, when specified, filters the results to include only the machines that support single sessions. .PARAMETER TotalServers This switch includes only machines that support multiple sessions. .PARAMETER RegisteredDesktops Filters to include only desktops that are registered and support single sessions. .PARAMETER RegisteredApps Includes only applications that are registered and support multiple sessions. .PARAMETER UnRegisteredDesktops Includes only desktops that are unregistered and support single sessions. .PARAMETER UnRegisteredApps Filters to include only applications that are unregistered and support multiple sessions. .PARAMETER GhostSessions Includes sessions that are stale but still powered on and not in maintenance mode. .PARAMETER GhostConnectedSessions Filter and count the sessions that are connected but are potentially hung or inactive for an unusually long time. These sessions appear to be active, as they maintain a connection status, but there may be little to no user activity, indicating that the session might have encountered an issue, such as a software freeze or network disruption. .PARAMETER AppMaintenanceMode Filters to include applications that are currently set in maintenance mode. .PARAMETER DesktopMaintenanceMode Filters to include desktops that are currently set in maintenance mode. .PARAMETER CitrixRPA Includes only robotic process automation sessions where it contains RPA in the machine name. .EXAMPLE Get-CitrixSystemStatus -AdminAddress "admin.example.com" -RegisteredDesktops Get count of all registered desktops from the admin server at "admin.example.com". .EXAMPLE Get-CitrixSystemStatus -AdminAddress "admin.example.com" -AppMaintenanceMode Get count of all machines in maintenance mode. .NOTES Requires administrative privileges to run.Ensure that the user executing the cmdlet has the necessary permissions. #> Function Get-CitrixSystemStatus { [CmdletBinding()] [OutputType([int])] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string]$AdminAddress, [Alias('TD')] [switch]$TotalDesktops, [Alias('TS')] [switch]$TotalServers, [Alias('RM')] [switch]$RegisteredDesktops, [Alias('RD')] [switch]$RegisteredApps, [Alias('RA')] [switch]$UnRegisteredDesktops, [Alias('UD')] [switch]$UnRegisteredApps, [Alias('UA')] [switch]$GhostSessions, [Alias('GCS')] [switch]$GhostConnectedSessions, [Alias('AMM')] [switch]$AppMaintenanceMode, [Alias('DMM')] [switch]$DesktopMaintenanceMode, [Alias('CRPA')] [switch]$CitrixRPA ) begin { Add-PSSnapin 'Citrix.Broker.Admin.*' -ErrorAction SilentlyContinue $ConnectionTimeoutMinutes = 5 $cutoff = (Get-Date).AddMinutes(-$ConnectionTimeoutMinutes) } process { try { $filterList = @() # Convert boolean parameters for filtering $trueStr = "`$true" $falseStr = "`$false" $nullStr = "`$null" if ($TotalDesktops) { $filterList += "SessionSupport -eq 'SingleSession'" } if ($TotalServers) { $filterList += "SessionSupport -eq 'MultiSession'" } if ($RegisteredDesktops) { $filterList += "RegistrationState -eq 'Registered' -and SessionSupport -eq 'SingleSession'" } if ($UnRegisteredDesktops) { $filterList += "RegistrationState -eq 'UnRegistered' -and SessionSupport -eq 'SingleSession'" } if ($RegisteredApps) { $filterList += "RegistrationState -eq 'Registered' -and SessionSupport -eq 'MultiSession'" } if ($UnRegisteredApps) { $filterList += "RegistrationState -eq 'UnRegistered' -and SessionSupport -eq 'MultiSession'" } if ($GhostSessions) { $filterList += "(PowerState -eq 'On') -and (SessionState -eq 'Active' -or SessionState -eq 'Disconnected') -and (SessionStateChangeTime -lt '$cutoff') -and (InMaintenanceMode -eq $falseStr) -and (SessionUserName -eq $nullStr)" } if ($GhostConnectedSessions) { $filterList += "(PowerState -eq 'On') -and (SessionState -eq 'Connected') -and (SessionStateChangeTime -lt '$cutoff')" } if ($AppMaintenanceMode) { $filterList += "InMaintenanceMode -eq $trueStr -and SessionSupport -eq 'MultiSession'" } if ($DesktopMaintenanceMode) { $filterList += "InMaintenanceMode -eq $trueStr -and SessionSupport -eq 'SingleSession'" } if ($CitrixRPA) { $filterList += "DesktopKind -eq 'Private' -and DesktopGroupName -like '*-RPA-*'" } $finalFilter = $filterList -join " -or " Write-Verbose "Querying Citrix system status from $AdminAddress..." $machines = Get-BrokerMachine -AdminAddress $AdminAddress -MaxRecordCount ([Int32]::MaxValue) -Filter $finalFilter -ErrorAction Stop return $machines.Count } catch { Write-Warning "Error querying $AdminAddress. Exception message: $_.Exception.Message" } } } |