Solved

Terminate User Sessions

Posted on 2004-08-25
10
487 Views
Last Modified: 2010-04-12
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
0
Comment
Question by:i601254
10 Comments
 
LVL 9

Expert Comment

by:jdeclue
ID: 11893480
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
0
 
LVL 5

Expert Comment

by:tebacher
ID: 11893585
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.
0
 
LVL 2

Expert Comment

by:Ranidae
ID: 11896398
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
0
 
LVL 2

Accepted Solution

by:
Ranidae earned 50 total points
ID: 11896425
Sorry... Go for this one... I forgot to clean the boot.ini section I use out of the one above.  Same instructions though.

'########################################
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
 
 '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
0
 
LVL 9

Expert Comment

by:jdeclue
ID: 11896510
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:Ranidae
ID: 11896907
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.
0
 
LVL 9

Expert Comment

by:jdeclue
ID: 11898545
Yeah, I am used to large environments and federal customers. Probably not a big deal.

J
0
 

Author Comment

by:i601254
ID: 11902075
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.
0
 
LVL 9

Expert Comment

by:jdeclue
ID: 11903016
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
0
 
LVL 2

Expert Comment

by:Ranidae
ID: 11908347
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!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

18 Experts available now in Live!

Get 1:1 Help Now