Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7602635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:27:46+00:00 2026-05-30T23:27:46+00:00

I need to find an easy way to kick off teammates from a server

  • 0

I need to find an easy way to kick off teammates from a server if they forget to log out. Our terminal service has a limit of 2 connections. I wrote the following function to get the logged in users. As I understand in powershell 2 there are nicer ways to do this but I am stuck with version 1 for now.

The following function will get me the logins per server. (If you know a prettier way to do this feel free to educate me. 🙂 I am not happy with the func and hoping learn a few tricks from you.)

function get-terminal-users($serverName)
{
    $path = "C:\ntui\source\i386"

    cd -path $path

    $result = invoke-expression -Command ".\quser.exe /SERVER:$serverName" | select -Skip 1       

    $a = @()

    foreach($row in $result)
    {        
        $d = new-object Object
        $columns = $row.Replace("  ", " ").Split(" ") | ? {$_ -ne ""}    

        $d | add-member -membertype noteproperty -name UserName -value $columns[0]    
        $id = -1
        $sessionName = ""                                  
        $columShift = 0

        #this part was not ok and needed fixing. Thanks Winfred for the answer!
        if([system.int32]::tryparse($columns[1].ToString(),[ref] $id))
        {     
            $columShift --              
        }
        else
        {            
            $sessionName =  $columns[1]
            $id = [system.int32]::parse($columns[2].ToString())                                             
        }          

        $time = [DateTime]::Parse($columns[5 + $columShift] + " " + $columns[6 + $columShift])

        $d | add-member -membertype noteproperty -name SessionName -value $sessionName
        $d | add-member -membertype noteproperty -name ID -value $id     
        $d | add-member -membertype noteproperty -name State -value $columns[3 + $columShift]
        $d | add-member -membertype noteproperty -name Idle -value $columns[4 + $columShift]
        $d | add-member -membertype noteproperty -name Time -value $time 

        $a += $d
    }

    return $a
}

So I need the list if users and then I have to kick one guy out if the connection count is higher than the allowed maximum. If you know a way to get this info from the server I would really appreciate your help.

$name = "myserver"
$maxConnections = 2

$users = get-terminal-users($name)   

if($users.Count -eq $maxConnections)
{
    $idList = $users | ? { $_.State -eq "Disc" } | select -First 1 | foreach { $_.ID }   

    foreach($id in $idList)
    {          
        invoke-expression -Command ".\logoff.exe /SERVER:$name $id -V"                  
    }
}

The issue is that I can only interact with Active connections. Abandoned connections are still use up the available connections and I don’t seem to be able to do much about them. A user with a Disc status seems to use up a terminal connection and when I call logoff on its id for some reason a local process is terminated. Is there a way to get rid of Disc connections?

rwinsta.exe also seems to only work with active connections.

I am running the script on XP and targeting a Win 2003 server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:27:47+00:00Added an answer on May 30, 2026 at 11:27 pm

    You add the $id incorrectly to the custom object at this line:

    $d | add-member -membertype noteproperty -name ID -value $id
    

    Disconnected sessions don’t have a session name see:

    quser.exe /SERVER:mycomputer
    USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
    winfred                                   2  Disc      4+21:05  2/28/2012 8:38 AM
    

    And require the $columShift, which you have on all your properties, except on the ID Noteproperty. Because of that, you specify the ID incorrectly to logoff.exe, when you are trying to log off a disconnected session.

    Instead that line should be:

    $d | add-member -membertype noteproperty -name ID -value $columns[2 + $columShift]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find an easy way to know if the local machine's 'automatically
I can't find an easy way to turn off word wrap in a JTextPane.
With the iOS SDK I need to find an easy and secure way to
I'm looking for an easy way to find out the distances in miles (as
i need to find out automationid for key board values? how to send keystrokes
I need to find the coordinates of windows open even when they are not
I need to find out the value passed into an indexer. My code (c#)
I need to find a way to get actual page size in Google Chrome.
I need an easy way to iterate over multiple collections without actually merging them,
I've racked my brain trying to find an easy way to do this and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.