Jump to content

[Help] Restart several PCs at a certain time


Recommended Posts

I found a vbscript to remotely restart PCs but it doesn't appear to work. What I want to do is from a text file that has a list of about 500 PC numbers I want to restart them at midnight on Saturday. I tried to create a scheduled task but it says it couldn't start. I created a batch file using the admin account on these PCs to execute the shutdown. Not 100% what went wrong so I thought maybe a vbscript would work and just read the PC numbers from the text file to reboot them remotely.

Link to comment
Share on other sites


Can you post the code for the VBs so we can see what could be wrong with it?

With regards to the Scheduled task, try using the AT command (at a command prompt), if you can create a working shutdown task from here then scripting a batch file should be no bother to deploy to you 500 PC's.

Also can you explain what you mean by PC numbers?? Do you mean IP addresses?

Edited by phiban
Link to comment
Share on other sites

The PC number is the computer name.

Here is the vb script I found:

Const EVENT_SUCCESS = 0

Set objShell = Wscript.CreateObject("Wscript.Shell")

objShell.LogEvent EVENT_SUCCESS, _

"Weekly scheduled restart of the SERVERNAME"

strComputer = "."

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" _

& strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

ObjOperatingSystem.Reboot()

Next

I'll try the AT command for my schedule task and see what happens.

Link to comment
Share on other sites

If you don't want to stick with vbs, you could try shutdown.exe (in the 2k resource kit and with XP/2K3) or psshutdown.exe (from sysinternals). All you need to do is a batch and use the task scheduler.

The batch could be:

for /f "delims=;" %%i in (listcomp.lst) do (psshutdown \\%%i -u user -p password -r -f)

and lisctomp.lst would be the list of the computername or ipadress (one per line). Note that the user and password are not needed if you use for the scheduled task a domain admin account and if the machine you want to reboot are all in the domain.

Link to comment
Share on other sites

Change this script

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" _
& strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Reboot()
Next

To this I have included some other Const

I have texted this script on my computer and it works.

Const LogOff = 0,  Shutdown = 1, Reboot = 2
Const ForcedLogOff = 4, ForcedShutdown = 5, ForcedReboot = 6
Const PowerOff = 8, ForcedPowerOff = 12
Dim WMISrv, StrOS, ObjOS
strComputer = "."
Set WMISrv = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & strComputer & "\root\cimv2")
Set ObjOS = WMISrv.ExecQuery("Select * from Win32_OperatingSystem")
For Each StrOS in ObjOS
StrOS.Win32Shutdown(ForcedReboot)
Next

The above script will reboot a computer but to do what you want you would have to add something like this.

This script is to read a text file

Const ForReading = 1
Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim TextFile : TextFile = "Test.txt"
Dim StrLine, Ts
Set Ts = Fso.OpenTextFile(TextFile,ForReading)

Do Until Ts.AtEndOfStream
StrLine = Ts.ReadLine
WScript.Echo StrLine
Loop

This script is a combination of the 2 above scripts.

Note this is only a guess and I cannot test this script

Const LogOff = 0,  Shutdown = 1, Reboot = 2
Const ForcedLogOff = 4, ForcedShutdown = 5, ForcedReboot = 6
Const PowerOff = 8, ForcedPowerOff = 12
Const ForReading = 1
Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
'<- PLACE THE FULL PATH TO THE TEXT FILE ->
Dim TextFile : TextFile = "Test.txt"
Dim ObjOS, strComputer, StrLine, StrOS, Ts, WMISrv
Set Ts = Fso.OpenTextFile(TextFile,ForReading)
'/-> START THE LOOPS TO READ THE TEXT FILE AND PASS EACH LINE AS STRCOMPUTER
Do Until Ts.AtEndOfStream
StrLine = Ts.ReadLine
strComputer = StrLine
Set WMISrv = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" &_
strComputer & "\root\cimv2")
Set ObjOS = WMISrv.ExecQuery("Select * from Win32_OperatingSystem")
For Each StrOS in ObjOS
StrOS.Win32Shutdown(ForcedReboot)
Next
Loop

All you would have to is schedule this to run from one computer acrooss the network.

Here is a Link to some wmi scripts. On the left side there a menu and select WMI Tasks: Scheduled Tasks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...