Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reboot Computers In Multiple OU's VBscript

Posted on 2009-04-06
15
Medium Priority
?
3,028 Views
Last Modified: 2012-05-06
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:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/2003_Server/Q_22053520.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

0
Comment
Question by:Jeffery Byers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
15 Comments
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24078868
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

0
 

Author Comment

by:Jeffery Byers
ID: 24078932
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!
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24079020
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.
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:Jeffery Byers
ID: 24079045
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
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24079098
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.
0
 
LVL 27

Accepted Solution

by:
bluntTony earned 1800 total points
ID: 24079619
Your code is basically sound. You need to perform a seperate query for each search base, return the computer names in the query recordset, then loop through these and issue the command to shut them down.
A couple of things
1. You didn't append the DNS suffix to the machine name, this is effectively the NetBIOS name - this may or may not be a problem for you, but you can append the DNS suffix to ensure it's not.
2. Personally I wouldn't worry about pinging the machine. If it's contactable you can shut it down. If it's not, then you can't. Just issue the command via the shell object but don't wait for the command to complete. If it shuts it down, great, if not, then you don't have to wait for a ping timeout. If the ping times out, all you're going to do is continue anyway. Currently your code isn't waiting for the run command to complete anyway and you'e got no feedback, so you've currently got nothing to benefit from pinging the machine. (my opinion at least :-) )
3. You can use psshutdown if you prefer, but the in-built shutdown.exe basically does everything you need. You can force a reboot, give a wait period and send a message to the machine to allow them to cancel if they need to.
4. If you're not waiting for the process to complete, I would add a periodic pause in your code so that you don't end up with hundreds of psshutdown.exe processes running on your machine. The code below currently pauses for 5 secs every 10 commands issues. You can change this as you see fit - this section is commented.
I haven't actually tested the shutting down of machines with this code (I may get sacked), so I haven't been able to check the pshsutdown syntax, but it successfully enumerates the machines in each OU specified in the strRoot array and appends the given DNS suffix (strDNSSuffix). Bear in mind that it also currently searches all sub OUs of each search base.
Let us know how you get on....

Dim strRoot(2)
Dim strFilter, strAttrs, strScope, strDNSSuffix, strBase
Dim objConn, objRS
 
strRoot(0) = "OU=Member Servers,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
		ShutDownComputer(objRS.Fields("name").Value & strDNSSuffix)
		'For every 10 records looped through. Pause for 5 secs.....
		If objRS.Bookmark Mod 10 = 0 Then
			WScript.Sleep 5000
		End If
		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

0
 

Author Comment

by:Jeffery Byers
ID: 24079882
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
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24079901
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

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24079926
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).
0
 
LVL 17

Assisted Solution

by:Jared Luker
Jared Luker earned 200 total points
ID: 24080035
Nicely done bluntTony... much cleaner...

I would disagree with you on the pinging though.  The script will run MUCH faster if you are pinging to check if the host is alive.  If you are starting the script before you go home and just walk away, then it's probably not an issue.  I usually start a script and watch it for a while to see if it's behaving.  I can't stand waiting for something to time out such as psshutdown.

If you just put ",true" at the end of the objShell.run line, it will make sure that only one psshutdown process is running at a time.  I prefer to do it that way.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24080335
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....
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24080407
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.
0
 

Author Comment

by:Jeffery Byers
ID: 24080511
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
0
 

Author Comment

by:Jeffery Byers
ID: 24084573
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

0
 

Author Closing Comment

by:Jeffery Byers
ID: 31567098
thanks!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question