We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

AD script to restart all computers in OU

qwert5905
qwert5905 asked
on
Medium Priority
1,475 Views
Last Modified: 2008-01-09
I'm looking for a script to restart all XP computers in a specific OU. Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2005
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2005

Commented:
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.
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
CERTIFIED EXPERT
Top Expert 2005

Commented:
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.
CERTIFIED EXPERT
Top Expert 2006
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!
CERTIFIED EXPERT
Top Expert 2005

Commented:
No, I haven't - let's have a look.
CERTIFIED EXPERT
Top Expert 2005

Commented:
Interesting...

CERTIFIED EXPERT
Top Expert 2006

Commented:
Maybe some of those tools will be incorped into the next server release, that would be sweet
CERTIFIED EXPERT
Top Expert 2005

Commented:
There are some of them already in different forms from MS - some (not many) of them leverage the same calls as MS tools.
CERTIFIED EXPERT
Top Expert 2006

Commented:
ah i see, Sysinternals just made them nicer to use...
CERTIFIED EXPERT
Top Expert 2005

Commented:
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.

CERTIFIED EXPERT
Top Expert 2006

Commented:
Indeed, exactly like the Active Roles Software from Quest i was playing with, just AD delegation with a nicer face :)

Author

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
CERTIFIED EXPERT
Top Expert 2005

Commented:
Change set /p to simply set.

set objOU=Training

Author

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
yes
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.