Solved

AD script to restart all computers in OU

Posted on 2006-11-08
18
1,197 Views
Last Modified: 2008-01-09
I'm looking for a script to restart all XP computers in a specific OU. Thanks!
0
Comment
Question by:qwert5905
  • 8
  • 4
  • 2
  • +1
18 Comments
 
LVL 51

Accepted Solution

by:
Netman66 earned 84 total points
Comment Utility
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
 
LVL 51

Expert Comment

by:Netman66
Comment Utility
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
 
LVL 7

Assisted Solution

by:CharliePete00
CharliePete00 earned 83 total points
Comment Utility
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
 
LVL 51

Expert Comment

by:Netman66
Comment Utility
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
 
LVL 48

Assisted Solution

by:Jay_Jay70
Jay_Jay70 earned 83 total points
Comment Utility
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
 
LVL 51

Expert Comment

by:Netman66
Comment Utility
No, I haven't - let's have a look.
0
 
LVL 51

Expert Comment

by:Netman66
Comment Utility
Interesting...

0
 
LVL 48

Expert Comment

by:Jay_Jay70
Comment Utility
Maybe some of those tools will be incorped into the next server release, that would be sweet
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 51

Expert Comment

by:Netman66
Comment Utility
There are some of them already in different forms from MS - some (not many) of them leverage the same calls as MS tools.
0
 
LVL 48

Expert Comment

by:Jay_Jay70
Comment Utility
ah i see, Sysinternals just made them nicer to use...
0
 
LVL 51

Expert Comment

by:Netman66
Comment Utility
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
 
LVL 48

Expert Comment

by:Jay_Jay70
Comment Utility
Indeed, exactly like the Active Roles Software from Quest i was playing with, just AD delegation with a nicer face :)
0
 

Author Comment

by:qwert5905
Comment Utility
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
 
LVL 51

Expert Comment

by:Netman66
Comment Utility
Change set /p to simply set.

set objOU=Training

0
 

Author Comment

by:qwert5905
Comment Utility
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
 
LVL 7

Expert Comment

by:CharliePete00
Comment Utility
yes
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

by Batuhan Cetin In this article I will be guiding through the process of removing a failed DC metadata from Active Directory (hereafter, AD) using the ntdsutil tool in a Windows Server 2003 environment. These steps are not necessary in a Win…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now