Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

AD script to restart all computers in OU

Posted on 2006-11-08
18
Medium Priority
?
1,286 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
16 Comments
 
LVL 51

Accepted Solution

by:
Netman66 earned 336 total points
ID: 17899997
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
ID: 17900062
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 332 total points
ID: 17900123
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 51

Expert Comment

by:Netman66
ID: 17900187
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 332 total points
ID: 17902036
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
ID: 17902064
No, I haven't - let's have a look.
0
 
LVL 51

Expert Comment

by:Netman66
ID: 17902122
Interesting...

0
 
LVL 48

Expert Comment

by:Jay_Jay70
ID: 17902275
Maybe some of those tools will be incorped into the next server release, that would be sweet
0
 
LVL 51

Expert Comment

by:Netman66
ID: 17902314
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
ID: 17902348
ah i see, Sysinternals just made them nicer to use...
0
 
LVL 51

Expert Comment

by:Netman66
ID: 17902553
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
ID: 17902614
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
ID: 17902888
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
ID: 17902912
Change set /p to simply set.

set objOU=Training

0
 

Author Comment

by:qwert5905
ID: 17902932
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
ID: 17906784
yes
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

564 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