Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-12
16
Medium Priority
?
2,196 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
Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
WARNING:   If you follow the instructions here, you will wipe out your VTP and VLAN configurations.  Make sure you have backed up your switch!!! I recently had some issues with a few low-end Cisco routers (RV325) and I opened a case with Cisco TA…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

670 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