Link to home
Start Free TrialLog in
Avatar of i601254
i601254

asked on

Terminate User Sessions

I need to find a method (script?) that will terminate all open user sessions at a specified time of night. this would be scheduled to run in Task Manager. Turns out some users aren't logging off correctly and leaving some connections to files open. This is causing problems with our business management application which runs various utilities every night, including reindexing our database. This has been failing since several files have been reported to have been in use and the application needs exclusive use of these files. Since several are still open, the file(s) are reported as "locked by another user".

Ive looked through MS scripting site, but haven't found anything yet.


Thank you,
Steve
Avatar of jdeclue
jdeclue

Here is an answer, but it doesn't use scripting.

If it is appropriate, you can set the users to not be able to Login for a period in there user accounts, then you can set a GPO that will force their disconnection after logon hours on a specific Acitve Directory container, that has the servers you need them to be kicked out of in it. The one problem with this method, is that while it will disconnect them from the server and their session, the user won't be able to logon to the network during the black out period.

Open a user account, select Account, and then click on Logon Hours. Remove the period of time needed.
Create a new OU for the servers, create a GPO on the OU. Set the policy:
Computer COnfiguration | Windows Settings | Local Policies | Security Options | "Force Logoff when logoff hours expire" Enable

J
I'm not sure what you want the users to log out of...? is it a terminal server or a fileserver or what?

I have a script that will work to log users off a terminal server, but I'm not sure if that's what you need.
Here is a vbscript that will do it for you:

Create a computernames.txt file with a list of all computernames that should be logged off. Call up the script from task scheduler and presto!

'###################################

Set oTS = oFSO.OpenTextFile("C:\computernames.txt")

'Set remote credentials
sUser = "administrator"
sPassword = "password"

'open list of client names


Do Until oTS.AtEndOfStream
 
 'get next client name
 sClient = oTS.ReadLine
 
Dim filesys, demofile
Set filesys = CreateObject ("Scripting.FileSystemObject")
Set demofile = filesys.GetFile("c:\boots\" & bootpath & "\boot.ini")
demofile.Copy("\\" & sClient & "\c$\")
 
 'get WMI locator
 Set oLocator = CreateObject("WbemScripting.SWbemLocator")

 'Connect to remote WMI
 Set oConnection = oLocator.ConnectServer(sClient, _
   "root\cimv2", sUser, sPassword)

  'issue shutdown to OS
 ' 4 = force logoff
 ' 5 = force shutdown
 ' 6 = force rebooot
 ' 12 = force power off
 Set oWindows = oConnection.ExecQuery("Select " & _
   "Name From Win32_OperatingSystem")
 For Each oSys In oWindows
   oSys.Win32ShutDown(4)
 Next

Loop

'close the text file
oTS.Close

'########################################3
ASKER CERTIFIED SOLUTION
Avatar of Ranidae
Ranidae
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Word of caution... never a good habit to put Username and password in a script, the priviliges required to log into a workstations WMI provider and issue a logoff command is local administrator. Ranidae do you know how to do this using the privilages of the scheduled job?

J
I'll be honest, I run the script from the server as a scheduled task.  The account used is added to the local admin groups of every machine to ensure that it works.  It's actually a domain users account that resides in a totally locked down OU.  Only it's membership in the local admin groups could be considered dangerous, and heck, that's what imaging is for. :)

Also, none of the users in my environment are savvy enough to cause any harm, specially since they mostly are never there when the script runs.
Yeah, I am used to large environments and federal customers. Probably not a big deal.

J
Avatar of i601254

ASKER

Thank you for all your responses. However, I should try and clarify...it's not really the physical pc's I need to disconnect, but rather user accounts that still have connections to open files on the server (under "computer management ->shared folders->open files"). I need to close all those open file connections prior to our one particular application running through it's night cycle.
Setting the logon period under the accounts, in AD and enabling force logoff after when logon period expires, will only remove the users connections to the servers that have the policy applied. So if you were to set a 15 minute period on the user account in which they can't log in (like, 1:45 - 2:00am), on the servers that you set the policy on, it will disconnect the user sessions at 1:45am.

J
And forcing the log off will also close all connections to the server... depending on the amount of users, it may be quicker to just use the script then set hours for each user indiviually.  Though if someone has a scripted method to change logon time, I'd love to see it!