Solved

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

Posted on 2012-04-12
16
2,078 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
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
 
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
Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now