AD script to restart all computers in OU

I'm looking for a script to restart all XP computers in a specific OU. Thanks!
qwert5905Asked:
Who is Participating?
 
Netman66Connect With a Mentor Commented:
Simple really.

Export a list of computers to text file from that OU.
Copy the computer names to the clipboard (one per line in the text file).

From the RUN box type, "shutdown /i"

You should see a Remote Shutdown applet.
Click Add.
Paste in your computers.
Click OK.
In the dropdown select Restart.
Tweak the other options as you see fit.
Click OK.

0
 
Netman66Commented:
When you export to txt file, you get a header line that can be deleted.

You also get the word "computer" after each computer name.  You can import this file into Excel so it splits it up and you can then select the first column only.

Or, better yet, use my script to create the text file.

Script on the way momentarily.
0
 
CharliePete00Connect With a Mentor Commented:
Or you can give the text file a name (like machines.txt) and excute the following from the command-line (be sure to replace machines.txt with the full path of the text file):

for /f %a in (machines.txt)  do shutdown -r -f -m \\%a

Or you can go the vbs/adsi/wmi route with something like the following:

' =====Begin Script=====>
Function RebootComputer(computer)
      Dim OpSysSet, OpSys, vReturn
      vReturn = -1

      Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//" & computer & _
            "/root/cimv2").ExecQuery(_
            "select * from Win32_OperatingSystem where Primary=true")
      for each OpSys in OpSysSet
              vReturn = OpSys.Reboot()
      next
      
      RebootComputer = vReturn
End Function


Const ADS_SCOPE_SUBTREE = 2

Dim ldapString, wqlCmd, orgUnitDn, varReturn
Dim objConnection, objCommand, objRecordSet

orgUnitDn = ""            ' Distinguished name of the OU ex - CN=Computers,DC=Fabrikam,DC=com

ldapString = "LDAP:" & orgUnit
wqlCmd = "SELECT Name FROM '" & ldapString & "'WHERE objectClass='computer'"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = wqlCmd
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    varReturn = -1
    varReturn = RebootComputer(objRecordSet.Fields("Name").Value)
    if varReturn <> 0 then
        Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value & " Reboot Failed"
    End If
    objRecordSet.MoveNext
Loop

'<=====End Script=====



Good Luck
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Netman66Commented:
Here is the script to get your computer names.

Create a new file with a dot CMD extension and copy this to it.


set /p objOU=Enter OU to export
for /f %%A in ('dsquery OU -name "%objOU%*"') do set objOUDN=%%A
for /f %%B in ('dsquery computer %objOUDN% -scope onelevel -limit 0 -o rdn') do @echo %%~B >> computers.txt

The computers.txt file will be created in the same place your .CMD file is located.

Use the txt file for your shutdown.
0
 
Jay_Jay70Connect With a Mentor Commented:
also another option is the psshutdown tool from sysinternals/MS

http://www.microsoft.com/technet/sysinternals/utilities/PsShutdown.mspx

NM - have you noticed sysinternals pages are now part of technet....crazy times!
0
 
Netman66Commented:
No, I haven't - let's have a look.
0
 
Netman66Commented:
Interesting...

0
 
Jay_Jay70Commented:
Maybe some of those tools will be incorped into the next server release, that would be sweet
0
 
Netman66Commented:
There are some of them already in different forms from MS - some (not many) of them leverage the same calls as MS tools.
0
 
Jay_Jay70Commented:
ah i see, Sysinternals just made them nicer to use...
0
 
Netman66Commented:
Exactly.  I use the analogy of the ADUC versus Hyena.  Hyena is much prettier, and incorporates all of the ADUC snap-in plus a lot more attributes.  The core of it still leverages the built-in stuff.

0
 
Jay_Jay70Commented:
Indeed, exactly like the Active Roles Software from Quest i was playing with, just AD delegation with a nicer face :)
0
 
qwert5905Author Commented:
Netman - I'm trying to start with the basics by getting the list of computer names via the script you sent (Thanks!).
The file appears to hang on the "Echo" command. I changed the first line to the OU: set /p objOU=Training.

Here's the output from the .cmd file:

C:\Temp>set /p objOU=Training
Training

It just hangs with no output.

Thanks,

Chris
0
 
Netman66Commented:
Change set /p to simply set.

set objOU=Training

0
 
qwert5905Author Commented:
CharliePete,

Thanks for the script. Is this the only line I need to modify: orgUnitDn = "". If so, this would be the correct entry (assuming sub-OU's)

orgUnitDn = "ou=CNK,ou=Finance West,dc=museum,dc=org"

Thanks,
Chris
0
 
CharliePete00Commented:
yes
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.