Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to automatically Reset/Logoff a terminal server session.

Status
Not open for further replies.

Pyro777

MIS
Jul 14, 2006
47
US
Well as it goes, the more you show the more they want... I am trying to write a script that will ask for the logged in users name, once entered in, the script will check all 10 of my terminal servers and if the user is found log them out (by using their session ID) My terminal server skill set it not where I would like it to be so any help would be great !!! Here is what I have so far.... I am using the Qwinsta.exe application to view the session information and do not yet have it checking against the user information or have the last part of the script to actually reset/logoff the user from the terminal servers. (Rwinsta)..

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("H:\inputlogs\termservers.txt", ForReading)
Set objshell = WScript.CreateObject ("WScript.Shell")

S0 = " /server:"
UserLogin = InputBox ("Type in the Users Login ID to log off from our terminal server farm")

Const ForReading = 1

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

'Then you can iterate it like this

For Each strLine In arrFileLines

objShell.Run "%comspec% /c qwinsta " & userlogin & S0 & Strline

WScript.Echo strLine

Next
 
A quick search revealed this thread: thread329-1308179

It show how to list all users logged into the server. You should be able to take that and use it to execute your logoff command if any of the users matches the input.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
I don't know your reasoning behind wanting this script, so I am not sure if the following will or will not be useful to you, but are you aware that you can configure a Terminal Server to automatically do this after a defined period of inactivity for a disconnected session?

You can configure this in Terminal Services Configuration or via Group Policy.

1. Log on to the terminal server as an administrator.

2. Start the Local Group Policy Editor. To do this, click Start, click Run, type gpedit.msc, and then click OK.

3. Locate the following node:

Computer Configuration\Administrative Templates\Windows Components\Terminal Services\Terminal Server\Session Time Limits


Note: The policy settings are also located under User Configuration\Administrative Templates\Windows Components\Terminal Services\Terminal Server\Session Time Limits

4. Enable Set time limit for logoff of RemoteApp sessions and set a disconnect time.


I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thank you for your input but I cannot access the Group Policy, due to it is "Not in my Job Description" and trying to get anything changed in this office is a long process. As for the other thread, it really wasnt what I needed...

How about this, I would like to run the qwinsta command against all 10 of my terminal servers and have the output displayed on one output box... By doing this, I can then add an input box for the server name and the session ID of the user I would like to log out.. This is not as automated as I would like but it will accomplish what I need it to do....
I was just trying to have it poll all of my servers on my .txt list by the user name i entered and once it found the user that I requested, log off that user... Could you help me with the syntax of the first part of this request ??

Thanks !!!
 
Here is my updated script... This script is only missing 2 components and then it will be completed. I would like for the script to write all output (all the servers it checks, onto a popupbox..) I would like to be able to see the script checked my servers and where it found the logged in session.
The second thing I would like to see is, everytime it checks a server and writes its output, I would like to have it place the server name (that was just checked) at the start of the output information (Possibly a header)

Can anyone help ???

Here is the script:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("H:\termservers.txt", ForReading)
Set objshell = WScript.CreateObject ("WScript.Shell")

S0 = " /server:"
UserLogin = InputBox ("Type in the Users Login ID to find the users Connected server and session ID")

'Spath = "net localgroup administrators " & strUser & S1
'Strpath = " /runpath:C:\"


Const ForReading = 1

Dim arrFileLines()




i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

'Then you can iterate it like this

For Each strLine in arrFileLines
' wscript.Echo ("cmd.exe /c C:\qwinsta " & userlogin & S0 & Strline )
objShell.Run "%comspec% /c qwinsta " & userlogin & S0 & Strline



' ScriptSessionId = GetSessionId()

'Call objShell.Run("cmd.exe /c C:\qwinsta.exe" & s0 & Strline )
WScript.Echo strLine

Next
SessionID = InputBox ("After the HTA runs, Please enter in the users Session ID")
ServerName = InputBox ("Now please enter in the Name of the Terminal Server the customer is connected to")
'cmdarg=("%comspec% /c logoff.exe " & SessionID & S0 & ServerName)
Call objShell.Run("cmd.exe /c" & "logoff " & SessionID & " /server:" & ServerName)

WScript.Echo ("c:\rwinsta " & SessionID & " /server:" & ServerName)



 
I may have a better way to approach this based on what I see you are doing in your code.

How big is your organization? How many users are typically logged onto a given Terminal Server?

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
We have approx 800 users, 4 sites and approx 15+ servers per site... There are a max of 7 users per terminal server as it is only hosting 2 applications.. Both applications are primary applications so those users need to be able to access the applications every day....

The perfect setup for me would be where I enter in the customers name, have qwinsta check all 10 servers for a response, if one is given, have that information (including the server name displayed on a msgbox) I can then enter in the session ID and server name to reset that user... I do not have access to those servers as they are in a different office and it is frowned upon, but I need to create a solution for my customer base.... The perfect script would search the servers for the login ID i provide, find that user and log them off automatically...
 
Here is what I would suggest. Use ASP.Net code. Create a page that has a drop down for your servers. Select a server and have that action bind to the server and enumerate the sessions on that server. Display each user with a check box next to their name. Allow multiple users to be selected at a time. Have a button for logoff that enumerates all the checked users and ends their session.

An alternative design is to have a text box. Enter the user name. Bind to each server one at a time, enumerate sessions and if one exists that matches the user name display it on screen. Move to the next server and search again. Use the same button design to bind to each user session and close it out.

What I don't understand though is how is it that you can connect with administrative rights to these sessions but can't launch Group Policy Manager on your own PC and simply connect to the remote servers to set the GPO? You don't have to have Active Directory rights, just administrative control over these servers.

If you don't have rights on the servers, then your script won't work either.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I do have administrator rights but am not allowed to touch the Group Policys setup on the production network or add new policies to the network... Getting the team that makes those changes is way to much red tape... I have never used asp.net before so I would not know where to start, do you have any good sites I can check out to learn ???

Thanks !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top