List all disconnected sessions that are running for 4 hrs and log off them

1.1k Views Asked by At

I am creating a script on how to list all the disconnected sessions that are running for 4 hrs but the code does not work.

Get-XASession | Select ServerName, State, Accountname, Browsername | where {$_.State -eq "Disconnected", $_.Duration -eq "4 hr"}

No error message is showing and not results as well.

But when I run:

Get-XASession | Select ServerName, State, Accountname, Browsername | where {$_.State -eq "Disconnected"}

It is working.

Need some help here. Thanks.

1

There are 1 best solutions below

1
On

You're not specifying an operator between your two conditions, instead you have a comma. Change your code to this:

Get-XASession | Select ServerName, State, Accountname, Browsername | where {$_.State -eq "Disconnected" -and $_.Duration -eq "4 hr"}

You can see this at play with this code:

1..10 | Where-Object {$_ -eq 2, $_ -eq 3}

vs

1..10 | Where-Object {$_ -eq 2 -or $_ -eq 3}

I've answered your question directly above. Here's something that you may want to try though, it should (hopefully) return all sessions older than 4 hours old (I don't have access to these cmdlets so it's a best guess)

$lowerThreshold = [datetime]::Now.AddHours(-4)
$session = Get-XASession | Select ServerName, State, Accountname, Browsername | where {$_.State -eq "Disconnected" -and $_.LogOnTime -le $lowerThreshold}`