Link to home
Start Free TrialLog in
Avatar of Clayton Pruett
Clayton PruettFlag for United States of America

asked on

Reboot Computers In Multiple OU's VBscript

Hi Experts,

I am trying to get a vb script that will reboot all computers in several OU's I have in our Server 2003 environment (all clients are XP SP3).

Here's what the script should do:
1. Iterate through several OU's
2. check/ping computer to make sure its available
3. if computer is up, reboot

I would prefer to use psshutdown because some of the options it offers (force, message to user, wait time, etc), but if thats not an option, its not a deal breaker.

I have tried this in a few different ways, and nothing has worked like I want, thought I could get some help here.

My vbs skills are so-so, but I know very, very little about WMI.

I *do not* want a batch file/script that I have to supply a txt file with all the computer to it in. We have that already, and we push out so many computers I would be generating a new txt file every week.

II have looked at the following examples, but can't piece it all together:
https://www.experts-exchange.com/questions/22053520/AD-script-to-restart-all-computers-in-OU.html?sfQueryTermInfo=1+10+comput+ou+reboot+vb
http://blog.netnerds.net/2006/12/vbscript-output-snippet
http://www.wisesoft.co.uk/scripts/vbscript_shutdown_all_computers_in_an_organizational_unit.aspx

Thanks, let me know if you need any more information!
'http://www.wisesoft.co.uk/scripts/vbscript_shutdown_all_computers_in_an_organizational_unit.aspx
 
OPTION Explicit
DIM cn,cmd,rs,i
DIM objRoot
DIM strFilter, strScope
 
' Specify OU(s) of computers you want to shutdown
DIM strRoot(3)
strRoot(0) = "ou=restart01,dc=domain,dc=local"
strRoot(1) = "ou=restart02,dc=domain,dc=local"
strRoot(2) = "ou=restart03,dc=domain,dc=local"
 
' Default filter for computer objects
' You might want to use a different filter.  By operating system for example:
' (&(objectCategory=Computer)(operatingSystem=Windows XP*))
strFilter = "(objectCategory=Computer)"
' Search child organizational units.  Use "onelevel" to search only the specified OU.
strScope = "subtree"
' *******************************************************************
 
'Start for loop to reboot computers in each of the OU's
For i = 0 to 2
	set cmd = createobject("ADODB.Command")
	set cn = createobject("ADODB.Connection")
	set rs = createobject("ADODB.Recordset")
 
	cn.open "Provider=ADsDSOObject;"
	cmd.activeconnection = cn
 
	cmd.commandtext = "<LDAP://" & strRoot(i) & ">;" & strFilter & ";" & _
			  "name;" & strScope
	'**** Bypass 1000 record limitation ****
	cmd.properties("page size")=1000
 
	set rs = cmd.execute
 
	while rs.eof <> true and rs.bof <> true
		'wscript.echo "Shutting Down " & rs("name") & "..."
		ShutDownComputer(rs("name"))
 
		rs.movenext
	wend
 
	cn.close
Next
 
	' Subroutine to shutdown a computer
	sub ShutDownComputer(byval strComputer)
		dim strShutDown,objShell
		' I played around with making sure the computer was pingable, but never got it to work. Here is what I was working off of:
                                          ' http://blog.netnerds.net/2006/12/vbscript-output-snippet/
                                          '
                                          'Set objWMIService = GetObject("winmgmts:\.\root\cimv2")
                                          'strComputer =  "myServer.myDomain.net"
                                          'Set colItems = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '" & strComputer & "'")
                                          '          For Each objItem in colItems
                                          '              If objItem.StatusCode = 0 Then 'The Computer is Pingable
                                          '              msgbox "Woot"
                                          '              End if
                                          '         Next
                                          'Set objWMIService = Nothing
		' -s = shutdown, -t 60 = 60 second timeout, -f = force programs to close
		strShutdown = "C:\bin\restart_computers\psshutdown.exe -r -f -c -t 300 -e p:0:0 -m " & chr(34) & "Nightly restart of computer" & chr(34) & " \\" & strComputer
 
			set objShell = CreateObject("WScript.Shell") 
 
			objShell.Run strShutdown, 0, false
 
	end sub

Open in new window

Avatar of Jared Luker
Jared Luker
Flag of United States of America image

Here is one that I did using the Exec method to ping and using psshutdown to do the actual rebooting.  You would just need to tweak it to work with your AD query.
SET objFSO = CreateObject("Scripting.FileSystemObject")
SET objInputfile = objFSO.OpenTextFile("ActiveHosts.txt")
SET objOutputfile = objFSO.CreateTextFile("log.txt", True)
 
 
'DO WHILE NOT objInputfile.AtEndOfStream
'strComputer = objInputfile.readline
SET objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
'WScript.Echo strPingResults
If InStr(strPingResults, "ttl") THEN
	WScript.echo strComputer' & " PING = YES "
	objshell.Run ("c:\pstools\psshutdown -f -r -t 03:00 \\" & strComputer
	Results = LCase(objExec.StdOut.ReadAll)
	WScript.Echo Results
Else
	objOutputfile.writeline strComputer & " PING = NO"
End IF
LOOP

Open in new window

Avatar of Clayton Pruett

ASKER

Hi Jared,

Thanks for the reply.

I see what you are doing, but I am stuck on how to tie this in with the results from the OU enumeration and only run this against machine in those OU's.

Any help with that part?

Thanks!
Well... I'm not 100% sure about the variables the person that wrote that is using, but it seems to me that you would want to put it inside that While...Wend loop.  You need to set whatever variable stores the current computer to be worked on as strComputer.  I think if you can get that figured out, as it runs through the loop for every machine it should reboot the ones that can be pinged and skip the ones that can not.
ok, i will try it out some today, and tonight will let it run in a few test ou's that are in production

if it works/doesnt work, i will let you know

thanks jared
I can use a script like this myself, so I'm trying to make it work as well.  I'll let you know if I don't hear from you first.
ASKER CERTIFIED SOLUTION
Avatar of bluntTony
bluntTony
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
bluntTony,

that looks perfect, exactly what i was looking for.

thanks for taking time to explain it as well, i appreciate it.

let me double check it tonight before i award points and call it done, but nice work!

thanks,

josh
If you want to first ping the machine quickly to check and make the script wait then the below code will do so. It's similar to jareds, but I've added a pause to wait for the objExec command to complete before deciding whether to shut it down, otherwise you might get a false negative on some machines.
Dim strRoot(2)
Dim strFilter, strAttrs, strScope, strDNSSuffix, strBase
Dim objConn, objRS, objShell,objExec
 
Set objShell = CreateObject("Wscript.Shell")
 
strRoot(0) = "OU=Member Servers,OU=Computers,OU=MyBusiness,DC=domain,DC=local"
strRoot(1) = "OU=Domain Controllers,DC=domain,DC=local"
strRoot(2) = "OU=Workstations,OU=Computers,OU=MyBusiness,DC=domain,DC=local"
 
strFilter = "(objectclass=computer);"
strAttrs  = "name;"
strScope  = "subtree"
 
strDNSSuffix = ".domain.local"
 
'This is your main loop, each time a different search base.
For i = 0 To UBound(strRoot) 
	strBase   =  "<LDAP://" & strRoot(i) & ">;"
	Set objConn = CreateObject("ADODB.Connection")
	objConn.Provider = "ADsDSOObject"
	objConn.Open "Active Directory Provider"
	Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
 
	objRS.MoveFirst
	'This is your inner loop, each time an individual PC found in the search of the base.
	While Not objRS.EOF
		Set objExec = objShell.Exec("ping -n 2 -w 1000 " & objRS.Fields("name").Value & strDNSSuffix)
		Do Until objExec.Status
			WScript.Sleep 250
		Loop
		strOutput = LCase(objExec.StdOut.ReadAll)
		If InStr(strOutput,"ttl") > 0 Then ShutDownComputer(objRS.Fields("name").Value & strDNSSuffix)
		objRS.MoveNext
	Wend
	
	Set objConn = Nothing
	Set objRS = Nothing
	
Next 'i
 
 
Sub ShutDownComputer(byval strComputer)
Dim strShutDown,objShell
 
strShutDown = "C:\bin\restart_computers\psshutdown.exe -r -f -c -t 300 -e p:0:0 -m " & chr(34) & "Nightly restart of computer" & chr(34) & " \\" & strComputer
Set objShell = CreateObject("WScript.Shell") 
objShell.Run strShutdown, 0, False
 
Set objShell = Nothing
End sub

Open in new window

Actually I think I was a bit hasty - you should ping the machine quickly before issuing the shutdown command. If psshutdown is anything like shutdown.exe, it will hang for a good 10 secs before deciding it can't contact the machine so it'd be quicker to do the ping test first.... (sorry - my bad).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, I think our posts may have crossed :) I sort of come to that conclusion after posting and have had to backtrack. I think I need to have some sleep....
For some strange reason, I'm getting  "Active Directory: An invalid directory pathname was passed"  AFTER it has echo'd all computers in the OU and sub-OU's.

Everything works fine except for that.
unlike jared, mine worked ok one one machine in one ou, but then on another machine in another test ou, i got a different error (this is for bluntTony's script that pings first)

either bof or eof is true, or the current record has been deleted. requested operation requires a current record
code: 800a0bcd
(see attachment)

i am googleing it now
reboot-error.jpg
ok, not sure what the difference was, but when i ran the script in a production ou, i didnt get the error.

i tested the script in a few different ways, and i attached what i came up with.

changes:
1. instead of useing the ping command to see if host was up, i used psshutdowns "-n" option for a timeout. with the ping command, the script took 9 minutes. with the "-n" option, it took 3 (both approximate times)
2. i reverted back to the pause every 10 records for 5 seconds tony. jared, i tried your suggestion of one instance of psshutdown, but that ran very, very slowly (even with the -n 1 option, it seemed to take 5 seconds per instance, which would have taken hours, unfortunately)

so, nice work, i will keep you posted if i find any more tweaks that seem to work better.

blunttony- excellent work, appreciate your help and explanations, i am giving you almost all the points
jared- thanks for your input, i think you would agree that tonys script is the answer, but i am giving some points to you for the input and help

thanks guys!

Dim strRoot(2)
Dim strFilter, strAttrs, strScope, strDNSSuffix, strBase
Dim objConn, objRS, objShell,objExec
 
Set objShell = CreateObject("Wscript.Shell")
 
strRoot(0) = "OU=test00,OU=Company,DC=domain,DC=local"
strRoot(1) = "OU=test01,OU=Company,DC=domain,DC=local"
strRoot(2) = "OU=test02,OU=Company,DC=domain,DC=local"
 
strFilter = "(objectclass=computer);"
strAttrs  = "name;"
strScope  = "subtree"
 
strDNSSuffix = ".domain.local"
 
'This is your main loop, each time a different search base.
For i = 0 To UBound(strRoot) 
	strBase   =  "<LDAP://" & strRoot(i) & ">;"
	Set objConn = CreateObject("ADODB.Connection")
	objConn.Provider = "ADsDSOObject"
	objConn.Open "Active Directory Provider"
	Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
 
	objRS.MoveFirst
	'This is your inner loop, each time an individual PC found in the search of the base.
	While Not objRS.EOF
		'For every 10 records looped through. Pause for 5 secs.....
		If objRS.Bookmark Mod 10 = 0 Then
			WScript.Sleep 5000
		End If
		ShutDownComputer(objRS.Fields("name").Value & strDNSSuffix)
		objRS.MoveNext
	Wend
	
	Set objConn = Nothing
	Set objRS = Nothing
	
Next 'i
 
 
Sub ShutDownComputer(byval strComputer)
	Dim strShutDown,objShell
	' Control the timeout with the "-n" option instead of pinging
	strShutDown = "C:\bin\restart_computers\psshutdown.exe -r -f -c -t 300 -e p:0:0 -n 1 -m " & chr(34) & "Nightly restart of computer" & chr(34) & " \\" & strComputer
	Set objShell = CreateObject("WScript.Shell") 
	objShell.Run strShutdown, 0, FALSE
 
	Set objShell = Nothing
End Sub

Open in new window

thanks!