Find-CitrixIdleUser.ps1 (Citrix Top 10)
This is one of the more useful scripts (at least for me.) It will query the farm and tell you all the user who have exceeded the idle time specified. I also combined another script that logged off users.
Name: Find-CitrixIdleUser.ps1
Purpose: Finds users with idle time greater than value passed and logs them off if -logoff is passed
-
# Find-CitrixIdleUser.ps1
-
# Brandon Shell [MVP]
-
# www.bsonposh.com
-
# Finds users with idle time greater than value passed
-
Param($time,[switch]$day,[switch]$hour,[switch]$minute,[switch]$logoff,[switch]$verbose,[switch]$help)
-
function HelpMe{
-
Write-Host
-
Write-Host " Find-CitrixIdleUser.ps1:" -fore Green
-
Write-Host " Finds users with idle time greater than value passed"
-
Write-Host
-
Write-Host " Parameters:" -fore Green
-
Write-Host " -Time : Optional. Server to Get Print Info From "
-
Write-Host " -Day : Optional. Checks Days of Idle Time "
-
Write-Host " -Hour : Optional. Checks Hours of Idle Time (Default)"
-
Write-Host " -Minute : Optional. Checks Minutes of Idle Time"
-
Write-Host " -Logoff : Optional. Logs User off if Idle Time exceeds Time"
-
Write-Host " -Verbose : Optional. Show Verbose Output"
-
Write-Host " -Help : Optional. Displays This"
-
Write-Host
-
Write-Host " Examples:" -fore Green
-
Write-Host " Finds Users who have been idle 2 hours and return AppName, UserName, SessionID, and ClientName" -fore White
-
Write-Host " Find-CitrixIdleUser.ps1 2 -hour | ft AppName,UserName,SessionID,ClientName -auto" -fore Yellow
-
Write-Host
-
Write-Host " Finds Users who have been idle 2 hours and Logs them Off" -fore White
-
Write-Host " Find-CitrixIdleUser.ps1 2 -hour -logoff" -fore Yellow
-
Write-Host
-
}
-
if($verbose){$verbosepreference = "Continue"}
-
Write-Host
-
if(!$time -or $help){helpme;Write-Host;Return}
-
-
# Get Citrix Farm Object
-
Write-Verbose " - Creating MFFarm Object"
-
$farm = new-Object -com "MetaframeCOM.MetaframeFarm"
-
$farm.Initialize(1)
-
-
# Parse Sessions
-
Write-Verbose " - Parsing Sessions. Total of [$($farm.Sessions.count)] Sessions"
-
foreach($session in $farm.Sessions)
-
{
-
Write-Verbose " - Processing Session ID [$($Session.SessionId)]"
-
$shouldLogOff = $false
-
-
# Getting Citrix Session Idle Time and Convert to System.DateTime
-
Write-Verbose " - Getting LastInputTime"
-
$ctxDate = $session.LastInputTime(1)
-
Write-Verbose " - Checking if idle time is -gt 0"
-
if(($ctxDate.HighPart -ne 0) -and ($ctxDate.LowPart -ne 0))
-
{
-
$date = "{0,4}{1,2:00}{2,2:00}{3,2:00}{4,2:00}" -f $ctxDate.year,$ctxDate.Month,$ctxDate.Day,$ctxDate.Hour,$ctxDate.Minute
-
Write-Verbose " - Converted LastInputTime to [$date]"
-
-
# Get Current Time in System.DateTime
-
Write-Verbose " - Getting Current Date"
-
$now = Get-Date
-
Write-Verbose " - Current Date is [$now]"
-
-
# Find Difference
-
Write-Verbose " + Getting Time Difference"
-
$diff = $now - $SessionIdleTime
-
Write-Verbose " - Found Days [$($diff.TotalDays)] Hours [$($diff.Totalhours)] Minutes [$($diff.TotalMinutes)]"
-
-
# Output Sessions that match
-
if($day) { if($diff.TotalDays -gt $time) {$session;$shouldLogOff = $true} }
-
if($hour) { if($diff.Totalhours -gt $time) {$session;$shouldLogOff = $true} }
-
if($minute){ if($diff.TotalMinutes -gt $time) {$session;$shouldLogOff = $true} }
-
-
Write-Verbose " - Set `$shouldLogOff to [$shouldLogOff]"
-
-
# Logging Off User
-
if($logoff -and $shouldLogOff)
-
{
-
Write-Verbose " - Logging Off Session ID [$($Session.SessionId)]"
-
$session.Logoff($false)
-
}
-
}
-
else
-
{
-
Write-Verbose " - Session ID [$($Session.SessionId)] NOT Idle"
-
}
-
Write-Host
-
}
tshell :: Apr.07.2008 :: All, Citrix, Powershell, Scripting :: No Comments »
