Skip to content

Instantly share code, notes, and snippets.

@kaimallea
Created November 23, 2010 21:48
Show Gist options
  • Save kaimallea/712602 to your computer and use it in GitHub Desktop.
Save kaimallea/712602 to your computer and use it in GitHub Desktop.
Citrix Query User
##################################################################
## Citrix User Query ##
## ##
## Usage: Invoke on a Citrix server with path to a text file ##
## containing server names, delimited by new line chars ##
##################################################################
# Only required parameter is a list of servers
param([string]$serverlist = $(Throw "No server list specified."))
# Email Config
$smtpHost = "smtp.domain.com"
$emailFrom = "[email protected]"
# Send-MailMessage must receive an array of strings for multiple recipients
$emailTo = @("[email protected]", "[email protected]")
# Fck Errors
trap {
writeLog $logPath "$(Get-Date) TRAPPED: $_"
Send-MailMessage -SmtpServer $smtpHost -From $emailFrom -To $emailTo -Subject 'Citrix User Query - Errors' -Body $_.Exception.Message
exit
}
# Path to store logs
$logDir = "$env:allusersprofile\CitrixUserQuery\"
$logPath = "$logDir\{0}-CitrixUserQuery.log" -f (Get-Date).ToString('MMddyyyy')
# Create $logDir if it doesn't exist
if (!(Test-Path -path $logDir -pathtype container)) {New-Item $logDir -type directory}
# Reusable logger
function writeLog {
param ([string]$logFile, [string]$txt)
# Create $logPath if it doesn't exist
if (!(Test-Path -path $logFile)) { New-Item $logFile -type file }
Add-Content $logFile $txt
}
# Get servers from flat file (should be separated by newline chars)
$servers = Get-Content $serverlist
$farmusers = 0 # Track total users on farm
$nodes = @() # Server-specific counts will be stored as objects
foreach ($server in $servers) {
$exp = "query user /server:$server"
$results = Invoke-Expression $exp
if ($results) {
$node = New-Object System.Object
$node | Add-Member -type NoteProperty -name Name -value $server
$node | Add-Member -type NoteProperty -name Users -value 0
foreach ($user in $results) {
if ($user.contains('ica')) {
$farmusers += 1
$node.Users += 1
}
}
$nodes += $node
}
}
$body = (Get-Date).toString() + "`n`n"
$body += "Total Active Users in Citrix Farm: $farmusers`n`n"
$body += $nodes | Sort-Object Users -descending | Format-Table -auto | Out-String
writeLog $logPath $body
Send-MailMessage -SmtpServer $smtpHost -From $emailFrom -To $emailTo -Subject 'Citrix User Query' -Body $body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment