Restart Computers Via VBS Script

Ok, so i found the following script online:
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:\Dept Restart\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(6)
 Next

Loop

'close the text file
oTS.Close
WScript.Echo "Machines Restarted!"

Now the clients.txt file that it references, if it only contains one machine name, the script works.  But when I try and include more than one machine it give me a "RPC server is unavailable" code: 800706BA, source: SWbemLocator, etc.  I'm a networking guy so forgive me, but it would seem to me I have the formatting incorrect on the txt file and so its crapping out.  Help please, thank you.

Ryan
LVL 1
stepstraightAsked:
Who is Participating?
 
GarylsConnect With a Mentor Commented:
I was kinda hoping the "Ok" box would just give you an indication of what the value of "sClient" is.

I suggest moving the lines:

  'get WMI locator
   Set oLocator = CreateObject("WbemScripting.SWbemLocator")

Above the "Do...Until" line and remove the new "WScript.Echo sClient" line

I will test the script out tomorrow on my own network and see if I can sort it...
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Any reason you want to use a VBScript and not a much simpler batch file or even PSSHUTDOWN from www.SysInternals.com
0
 
slow1000Commented:
Have you checked out the windows utility shutdown (in command prompt, do "shutdown /?" to see options, it is rather useful).
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Wouter BoevinkMasterCommented:
What's the contents of your textfile?
One machinename per line?
0
 
GarylsCommented:
Are you sure its your code?

Might be worth checking the machine is actually alive and the correct ports (445? etc) are open in the Windows firewall for RPC.

0
 
stepstraightAuthor Commented:
Yes i've tried to two you mention, didn't care for them.  I have tried man variations of entering the data in the text file, comma seperated, tab seperated, one per line ,etc.  Windows firewall is disabled via GPO in our org so that is not the issue, machines are always live.  As I stated in the question, when I only have one machine in the text file it works, regardless of what machine it is, but when multiples are in the text file it craps out.  Does anyone know the correct format so the script will read it correctly?
0
 
GarylsCommented:
Soz, wasn't sure you'd tried it with multiple machines using a single entry in the text file.

The line:

  sClient = oTS.ReadLine

Is supposed to seperate at carriage returns (CrLf to be more precise).

Try doing:

  WScript.Echo sClient

after sClient = oTS.Readline to make sure its actually getting the right value.

Also the code:

'get WMI locator
 Set oLocator = CreateObject("WbemScripting.SWbemLocator")

Should probably not be in the loop, it should be before the "Do While AtEndOfStream". You only need a single instance of this object (I think)

0
 
stepstraightAuthor Commented:
Ok so i made the change:

WScript.Echo sClient

after sClient = oTS.Readline to make sure its actually getting the right value.

Now it works having multiple machines listed in the text file via carriage return.  It now prompts an "ok" box as it chews through each machine in my list.  Any way to make it just display the final "Machines restarted", and not give me a "play by play" so to speak?  thanks.
0
 
stepstraightAuthor Commented:
That seemed to have done the trick, I'm going to test it out tonight fully on the 30 workstations I have slated for mass reboot and will report back in the morning.  If all goes well I'll then disperse the points accordingly, thanks for the help. :)
0
 
GarylsCommented:
Any joy? If not lemme know and I'll test it out here for you :)
0
All Courses

From novice to tech pro — start learning today.