Solved

AD script to restart all computers in OU

Posted on 2006-11-08
18
1,236 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
[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
  • 8
  • 4
  • 2
  • +1
18 Comments
 
LVL 51

Accepted Solution

by:
Netman66 earned 84 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 83 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 83 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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