Cheers,
I need your help by creating a new powershell script. Our technical “problem” is currently as following:
Our customers rdp session is always active because our app is sending and receiving packages from the client every 5 minutes. So they will never get logged off. For this case I already have a powershell script.
I need now an attempt for this issue:
We still have some customers which log in twice, sometimes three times.
Now I want to create a script with following method:
- Check all servers if a user is listed on two servers
- Compare both entries
- Logoff the one which is older
Sorry for my bad english, it is not my main language
Thank you in advance, I hope you can help me in this case
Important information I didn’t wrote:
The script goes across 8 servers. So I need to check 8 servers for the username.
Import-Module PSTerminalServices
$servers = 'kbo4-ts01', 'kbo4-ts02', 'kbo4-ts03', 'kbo4-ts04'
$servers | foreach {
Get-TSSession -Computername $_ -filter {$_.ConnectionState -eq "Active"} | fl *
}
I don’t know how I get now the check “get multiple entries and logoff the older entries”. I know it’s at the end working with Stop-TSSession but I stuck at the parameters and filter-entries.
So currently I am logged in on 4 servers with my username.
Now my next step is “compare all LoginTimes and list all SessionIds where the LoginTime is lower than the newst LoginTime”.
Hey Michael, as I understand it, you want to look on the list of servers for sessions that are logged in with the same account as the current user in the environment running the script. Then only list the older sessions. I’ve added comments to the sections below to explain what’s being done and why.
Import-Module PSTerminalServices
$servers = 'kbo4-ts01', 'kbo4-ts02', 'kbo4-ts03', 'kbo4-ts04'
$servers |
foreach {
# Changed filter to filter for username of the user in the current environment
Get-TSSession -Computername $_ -filter {$_.Username -eq [Environment]::UserName}
} |
# Use Select-Object to get the Properties you want from the session object
Select-Object @{Label="Server"; Expression={$_.Server.ServerName}}, SessionID, ConnectTime |
# Sort the results by connection time so that the newest connection is on top
Sort-Object ConnectTime -Descending |
# Use Select-Object to skip the first object in the list so only the older connections remain
Select-Object -Skip 1
Hey Curtis,
Thank you for your reply. This is almost correct. My goal is as following:
Username testuser1 is connected on 4 servers, my script now should find all 4 connections, list the 3 oldest one and logoff them so the newest one keeps connected. But nothing should happen if the user is connected only once.
Now I had time to try your code. It shows my goal. Now I just need to disconnect the result. Thank you!
I think I need a foreach-command for my following problem:
Now I have the correct results - thank you again, Curtis - but it just disconnects my first entry when I add “Stop-TSSession -Force” at the end and abort with the following errormessage:
Exception calling “Logoff” with “1” argument(s): “The system could not find provided file”
At C:\Users\AdminMichael\Documents\WindowsPowerShell\Modules\PSTerminalServices\PSTerminalServices.psm1:631 Zeichen:21
-
$session.Logoff <<<< ($Synchronous)
- CategoryInfo : NotSpecified: ( , ParentContainsErrorRecordException
FullyQualifiedErrorId : DotNetMethodException;
I need to edit my question. I tried it but I don't get the correct code. I don't want to check MY username on every server. I need to check all current logged in users if they are multiple times logged in.I try it the whole day but I am just not experienced enough in powershell
@michael-cekic, use the pre tags when posting code. It will make it easier to read.
With that said, I would take the following approach
- Query the 1st server for active sessions
- Use the returned data to build a hash table based on the username
IE.
$usersessions.administrator = @(-server session1-)
$usersessions.user1 = @(-server session1-)
- Query the 2nd server for active sessions
- Use the returned data to add to the existing hash table
IE.
$usersessions.administrator += @(-server session2-)
$usersessions.user3 += @(-server session2-)
- Repeat steps 3 and 4 for Server 3 and 4
- Now that all of the sessions have been collected, go through your hash table taking action on the older sessions using the method already shown previously.
Give this a try and post your code and we can work on what you have come up with if you run into trouble.