Solved

Windows 2008, logoff sessions from Terminal Server by USERNAME - command line ?

Posted on 2012-04-12
16
2,136 Views
Last Modified: 2012-04-19
Is it possible to logoff users via command line by USERNAME not session ID
 
Windows 2008.
 
Thank you
0
Comment
Question by:the_omnific
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 1

Author Comment

by:the_omnific
ID: 37840401
Or logoff all users via command line
0
 
LVL 4

Expert Comment

by:Red_Tech
ID: 37840420
0
 
LVL 1

Author Comment

by:the_omnific
ID: 37840439
That works but it doesn't logoff disconnected sessions?
0
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
LVL 6

Expert Comment

by:GregArnott
ID: 37840497
0
 
LVL 1

Author Comment

by:the_omnific
ID: 37840502
I have been through all this. Can someone please tell me the exact command that would work in a Windows Server 2008 environment to logoff /reset disconnected sessions via a command line :(
0
 
LVL 6

Expert Comment

by:GregArnott
ID: 37840509
Didn't specify the command line option(s) for [username] end, sorry.

Easiest way I know is PsTools:
psloggedon
psshutdown -o

http://technet.microsoft.com/en-us/sysinternals/bb897541
0
 
LVL 1

Author Comment

by:the_omnific
ID: 37840524
psshutdown -o doesn't logoff /reset disconnected sessions?
0
 
LVL 6

Expert Comment

by:GregArnott
ID: 37840623
Ah no. No it doesn't. Hmm, yeah, just for active sessions.

The only sure way I can think of via command line is the session based...

net use /user:[username] \\servername\share
query session /server:servername
reset session [ID] /server:servername

Another interesting means to achieve your goals would be to alter the group policy settings via command line, setting a very short timeout on disconnected sessions. Definitely one that's easier to achieve via qpedit.msc, but doable via console.

|edit:| Here's the command:
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" /v MaxDisconnectionTime /t REG_DWORD /d "60000" /f

Open in new window

|edit:end|
0
 
LVL 1

Author Comment

by:the_omnific
ID: 37840711
Thanks I knew those commands. I'm keen to know a way via command line not GPO
0
 
LVL 6

Expert Comment

by:GregArnott
ID: 37841006
I'm just curious why you can only use <username>, or why you cannot use:

query user <username>
logoff [id] // where [id] is returned from query.
0
 
LVL 1

Author Comment

by:the_omnific
ID: 37841029
I simply want to logoff all disconnected sessions from a command line. :(
0
 
LVL 6

Expert Comment

by:GregArnott
ID: 37841266
When you asked for it to be based off username, I thought you wanted it to be based off username for some reason... ie specific session termination; which I mentioned in previous comment - a command line means to disconnect a specific session querying their username to find session id.
Another command line kill of specific session:
get session:
   qwinsta /SERVER:servername
remove session:
   rwinsta {sessionname | sessionid} /SERVER:servername

Compliments of this.
Here's a script which will find all disconnected sessions and terminate them.
' logoff.vbs
' Finds all active TS sessions on server HOST defined
' below and terminates them.  Does not terminate the
' current session if being run remotely.
' If running on local host, set HOST value to ""

'// Added to Create and Open a file to write to
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile(".\ErrorLog.txt", True)

Host = ""
' Get disconnected sessions and log them off
sessions = DisconnectedSessions(Host) 
For each session in sessions 
    objOutputFile.WriteLine("sessionId = " & session) 
    TerminateWinSession Host, session
Next 

objOutputFile.WriteLine("Done.")
objOutputFile.Close
WScript.Quit(0)

Sub TerminateWinSession(Host, sessionId)
    Dim Sh, tmpHost
    Set Sh = createobject("WScript.Shell")
    If trim(Host)="" Then
        tmpHost = ""
    Else
        tmpHost = " /SERVER:" & Host
    End If
    objOutputFile.WriteLine("Executing... " & vbCrLf & "%COMSPEC% /C rwinsta " & sessionId & tmpHost & vbCrLf)
    Err.Clear
    Sh.Run "%COMSPEC% /C rwinsta " & sessionId & tmpHost, 0, False
    If Err <> 0 Then
        objOutputFile.WriteLine("Error: " & Err.Number & " " & Err.Description)
    End If
End Sub

Function DisconnectedSessions(Host) 
    Dim tmpHost, aTmp, aTmp1(), i , DiscCount
    If Trim(Host)="" Then 
        tmpHost = "" 
    Else 
        tmpHost = " /SERVER:" & Host 
    End If 
    objOutputFile.WriteLine("Executing... " & vbCrLf & "qwinsta" & tmpHost & " | find ""Disc""" & vbCrLf) 
    aTmp = Split(cmd("qwinsta" & tmpHost & " | find ""Disc"""),vbCrLf) 
    ReDim aTmp1(LBound(aTmp) to UBound(aTmp)) 
    DiscCount = 0
    For i = 0 to UBound(aTmp) 
        If Left(aTmp(i),1) <> ">" Then 
             aTmp1(DiscCount) = Val(Mid(aTmp(i), 42, 6)) 
             DiscCount = DiscCount + 1
        End If 
    Next 
    Redim Preserve aTmp1(LBound(aTmp) To (DiscCount - 1)) 

    DisconnectedSessions = aTmp1 
End Function 

Function Cmd(cmdline)
' Wrapper for getting StdOut from a console command
    Dim Sh, FSO, fOut, OutF, sCmd
    Set Sh = createobject("WScript.Shell")
    fOut = objFSO.GetTempName
    sCmd = "%COMSPEC% /c " & cmdline & " >" & fOut
    objOutputFile.WriteLine("Executing... " & vbCrLf & sCmd & vbCrLf)
    Err.Clear
    Sh.Run sCmd, 0, True
    If Err <> 0 Then
        objOutputFile.WriteLine("Error: " & Err.Number & " " & Err.Description)
    End If
    If objFSO.FileExists(fOut) Then
        If objFSO.GetFile(fOut).Size>0 Then
            Set OutF = objFSO.OpenTextFile(fOut)
            Cmd = OutF.Readall
            objOutputFile.Write(Cmd & vbCrLf)
            OutF.Close
        End If
        objFSO.DeleteFile(fOut)
    End If
End Function

Open in new window

0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 37841822
Have you try "net session"? You can do it from your desktop with "qwinsta" (Query) or "rwinsta" (Kill).

I don't think you can kill sessions based on username, regardless, you can see which username assoicated with the session anyway.

Hope this help.
0
 
LVL 1

Accepted Solution

by:
the_omnific earned 0 total points
ID: 37845451
In the end I wrote the following script for session id's 1-30.

logoff <session id> /server:<servername>

I knew this code anyway. It's not ideal but does the job :)

Thanks for everyone's input
0
 
LVL 6

Expert Comment

by:GregArnott
ID: 37849573
...
so you didn't like the script that detects disconnected sessions and terminates them?
/shrug.
0
 
LVL 1

Author Closing Comment

by:the_omnific
ID: 37864808
I used my own script
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question