XenDesktop PoSh script to enurate sessions and respond accordingly
Code
asnp Citrix* -ErrorAction SilentlyContinuefunction Receive-Info
{
process { Write-Host $_ -ForegroundColor DarkGreen }
}
function Receive-Warning
{
process { Write-Host $_ -ForegroundColor Red}
}
$ActiveSessions = Get-BrokerSession -Filter {(SessionState -eq "Active")} | Sort-Object -Property MachineName
cls
Write-Output ("*** Total Active Sessions: " + ($ActiveSessions | measure).Count + " ***")
ForEach ($BrokerSession in $ActiveSessions)
{
# Check to see if the user has had the session In-Use for an unusually long time (2 hrs)
$TimeInState = New-TimeSpan -Start ([datetime]$BrokerSession.SessionStateChangeTime) -End (Get-Date)
$TimeInStateHrs = [math]::Round($TimeInState.TotalHours,2)
if ($TimeInStateHrs -gt 4)
{
Write-Output ($BrokerSession.UserName `
+ " on " + $BrokerSession.MachineName `
+ "(" + [System.Net.Dns]::GetHostAddresses($BrokerSession.DNSName) + ")" `
+ ", Connected:" + [datetime]$BrokerSession.SessionStateChangeTime `
+ "; UID:" + $BrokerSession.UID `
+ ";" + $TimeInStateHrs + " hrs") | Receive-Warning
Write-Output ("Disconnecting Session " + $BrokerSession.SessionUid) | Receive-Warning
Disconnect-BrokerSession -InputObject $BrokerSession -Verbose
# Stop-BrokerSession -InputObject $BrokerSession -Verbose
}
else
{
Write-Output ($BrokerSession.UserName `
+ " on " + $BrokerSession.MachineName `
+ "(" + [System.Net.Dns]::GetHostAddresses($BrokerSession.DNSName) + ")" `
+ ", Connected:" + [datetime]$BrokerSession.SessionStateChangeTime `
+ "; UID:" + $BrokerSession.UID `
+ ";" + $TimeInStateHrs + " hrs") | Receive-Info
}
# Check for to see if Event 1039 occurred since the last time the user was able to successfully log in or reconnect
$CriticalEvents = Get-WinEvent -Computer $BrokerSession.DNSName -FilterHashTable @{LogName="Application";ProviderName="Citrix Desktop Service";Id=1039} -EA SilentlyContinue
if ((($CriticalEvents | measure).Count) -gt 0)
{
Write-output ("Event 1039 occurred " + ($CriticalEvents | measure).Count + " times")
ForEach ($CriticalEvent in $CriticalEvents)
{
Write-Output ([datetime]$CriticalEvent.TimeCreated) | Receive-Info
if ([datetime]$CriticalEvent.TimeCreated -gt [datetime]$BrokerSession.SessionStateChangeTime)
{
Write-Output ("Event " + $CriticalEvent.Id + " on " + [datetime]$CriticalEvent.TimeCreated + " occured AFTER reconnecting @ " + [datetime]$BrokerSession.SessionStateChangeTime)
Write-Output ("Resetting Session " + $BrokerSession.SessionUid) | Receive-Warning
Disconnect-BrokerSession -InputObject $BrokerSession -Verbose
Stop-BrokerSession -InputObject $BrokerSession -Verbose
}
}
}
}
Write-Output ("*** End Report for " + ($ActiveSessions | measure).Count + " active sessions ***")
# Check for Disconnected sessions beyond 3 hrs
$DiscSessions = Get-BrokerSession -Filter {(SessionState -eq "Disconnected")} | Sort-Object -Property MachineName
Write-Output ("-------------------------------------------------------------------------------") | Receive-Info
Write-Output ("*** Total Disconnected Sessions: " + ($DiscSessions | measure).Count + " ***")
ForEach ($BrokerSession in $DiscSessions)
{
# Check to see if the user has had the session In-Use for an unusually long time (2 hrs)
$TimeInState = New-TimeSpan -Start ([datetime]$BrokerSession.SessionStateChangeTime) -End (Get-Date)
$TimeInStateHrs = [math]::Round($TimeInState.TotalHours,2)
if ($TimeInStateHrs -gt 3)
{
Write-Output ($BrokerSession.UserName `
+ " on " + $BrokerSession.MachineName `
+ "(" + [System.Net.Dns]::GetHostAddresses($BrokerSession.DNSName) + ")" `
+ ", Connected:" + [datetime]$BrokerSession.SessionStateChangeTime `
+ "; UID:" + $BrokerSession.UID `
+ ";" + $TimeInStateHrs + " hrs") | Receive-Warning
Write-Output ("Resetting Session " + $BrokerSession.SessionUid) | Receive-Warning
Disconnect-BrokerSession -InputObject $BrokerSession -Verbose
Stop-BrokerSession -InputObject $BrokerSession -Verbose
}
}
Write-Output ("*** End Report for " + ($DiscSessions | measure).Count + " Disconnected sessions ***")
Write-Output ("-------------------------------------------------------------------------------") | Receive-Info
No comments