Solved

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

Posted on 2012-04-12
16
2,111 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Backup and host a VM 6 31
VPN Ports 8 33
Force local Group Policy instead of getting GP from domain for remote computers 19 28
Setup small office network 1 27
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This article explains how to install and use the NTBackup utility that comes with Windows Server.
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

820 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