Jump to content
Welcome to our new Citrix community!
  • 0

Powershell script to disconnect users idle > 15 mins

Daniel Wheeler


Just hoping to get someone to sanity check my syntax and/or logic, I've never written a PowerShell script for Citrix before.


A little background: My organization has issues with licensing on a specific custom-made application where we can't allow more than X users to be on at a time. That's a touchy political issue that I personally can't touch. I was asked to come up with a script that checked for users idle on that application more than 15 minutes and disconnect them (the farm that the app runs on is hosting ONLY that app). 


I came up with this:


Add-PSSnapIn Citrix.Common.Commands
Add-PSSnapin Citrix.XenApp.Commands
$sessions = get-xasession -Full | where {$_.browsername -eq "APPNAME" -and $_.state -ne "Listening" -and $_.state -ne "Disconnected" -and $_.SessionName -ne "Console"}
if ($sessions.count -gt 800)
    $sessions | where {($_.currenttime - $_.Lastinputtime).minutes -gt 15} | Disconnect-XASession


It seems to work, but as I said, I was hoping to get some advice or comments as this is my first foray into Citrix on Powershell.


Thank you.

Link to comment

7 answers to this question

Recommended Posts

  • 0

Hi Dan,


I'm also very new to CTX powershell.


Can you help explain the logic behind this check:  ($sessions.count -gt 800)

Are you looking to see if there are more than 800 session counts, and if so then disconnect?


I need a similar script as well.

Basically we have a very expensive application, and limited licenses.

People get on and don't let go.

Basically, we identify people that have been logged onto the app longer then x number of hours and call them to see if they are still using the app.


We cannot use GPOs to disconnect idle sessions since there are other applications that require long processes, and IT management does not want to spin up a separate farm to only support the one application. 


With all that said, basically I want to check to see if a user has been idle for more than 2 hours, and if so disconnect their session.


Finally, have you refined your script since last posting?  If so, would you mind adding it back out to the forum?

Link to comment
  • 0

I am working on a solution to achieve the same, for both XenApp 6.5 and 7.6 (7.6 is way more difficult because ICA session doesn't have idletime property anymore)


Dan Wheeler: don't use "seconds", use "totalseconds" instead. seconds returns only seconds part of the time. if idle is 2:35, it will return 35.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...