Wednesday, September 28, 2005

Shutting down remote PCs

Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection, oSys
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
oSys.Win32ShutDown(5)
Next
Loop
'close the text fileoTS.CloseWScript.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.

No comments:

 
Jeffrey Ting Jeffrey Ting on Facebook Jeffrey Ting on Spock Jeffrey Ting on Plaxo Jeffrey Ting on Spoke Jeffrey Ting on LinkedIn