Tips and Tricks
This script allows logoff and shut down of remote users.
After my February feature, “Automate Your Security,” with
VBScript, I received a lot of requests for a script that would remotely
log off remote users and even remotely shut down their computers. What
a great idea! So, I decided to see if it could be done with VBScript and
Windows Management Instrumentation (WMI)—and it can. Following is
the script you can use.
Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection,
Dim sUser, sPassword
'set remote credentials
sUser = "Administrator"
sPassword = "password"
'open list of client names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("C:\clients.txt")
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
'close the text file
WScript.Echo "All done!"
You should know a couple of things about this script. First, you need to have a text file named C:\Clients.txt on the machine where the script runs. That file should contain the list of client names you want to shut down. Instead of using a text file, you can modify this script to use ADSI, enabling it to query computer names from Active Directory or an NT domain. However, as you probably don’t want to run this script against every computer on your network (shutting down all of your servers might be bad), the text file provides you with complete control over which computers the script will affect.
Note that I’ve hard-coded a name and password into this script. That
usually isn’t necessary, provided the account you use to run the script
has administrative privileges on all the clients it’ll try to shut down.
Regardless, don’t leave the script lying around with an actual username
and password typed in!
|This script can be scheduled, using Windows’
Task Scheduler, to execute automatically. Run it from
your management workstation each evening and you’ll
be sure every workstation is properly logged off, shut
down or whatever.
Finally, note that this script is currently set to force a shutdown,
using 5 in the call to Win32ShuDown(). As noted in the script, you can
change that to 4, 6 or 12 to force a logoff, restart or power off, respectively.
This script should work with all 32-bit versions of Windows back to, and including, Windows NT 4.0 Service Pack 4. That means any NT boxes you have sitting around, along with all Windows 2000, Windows XP and Windows Server 2003 machines should respond to this script. This script relies on remote machines having the WMI service installed and running. If you’ve modified the default settings for that service and it isn’t running on your computers, then this script won’t be helpful to you.
With more than fifteen years of IT experience, Don Jones is one of the world’s leading experts on the Microsoft business technology platform. He’s the author of more than 35 books, including Windows PowerShell: TFM, Windows Administrator’s Scripting Toolkit, VBScript WMI and ADSI Unleashed, PHP-Nuke Garage, Special Edition Using Commerce Server 2002, Definitive Guide to SQL Server Performance Optimization, and many more. Don is a top-rated and in-demand speaker and serves on the advisory board for TechMentor. He is an accomplished IT journalist with features and monthly columns in Microsoft TechNet Magazine, Redmond Magazine, and on Web sites such as TechTarget and MCPMag.com. Don is also a multiple-year recipient of Microsoft’s prestigious Most Valuable Professional (MVP) Award, and is the Editor-in-Chief for Realtime Publishers.