Solved

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

Posted on 2012-04-12
16
2,062 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

Join & Write a Comment

Don’t let your business fall victim to the coming apocalypse – use our Survival Guide for the Fax Apocalypse to identify the risks and signs of zombie fax activities at your business.
Data center, now-a-days, is referred as the home of all the advanced technologies. In-fact, most of the businesses are now establishing their entire organizational structure around the IT capabilities.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
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…

747 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

15 Experts available now in Live!

Get 1:1 Help Now