Solved

Automatically Shutdown of computers in OU

Posted on 2009-04-04
9
1,042 Views
Last Modified: 2013-05-20
I have written a cmd script which effectively shuts down all computers in a specific OU on my active directory however it takes about an hour to run because of the 2 thousand computers that are in the OU.  

Many of them are already off so the shutdown command take a few seconds to effectively timeout.  The script runs the SHUTDOWN command on all computers in an OU.  Is there any way to query the active directory to issue the SHUTDOWN command only to computers that are really running and connected to the network with an established session?  

Thanks

My script
0
Comment
Question by:afeind
9 Comments
 
LVL 8

Expert Comment

by:halejr1
Comment Utility
afeind -- I hope you find the answer that you need, however couldn't you place your script in the netlogon script location, set an AT command calling that script from all machines, so that the process will run local and not be pushed out?

This way the script will only run on machines that are running as the AT "Schedule" will be triggered by a time event (daily, 9:00 pm or something like that) -- so if a machine is on, it will call the script, if a machine is off, no need to call the script.

Let me know what you think.

Cheers.
0
 

Expert Comment

by:someITguys
Comment Utility
It may be easier to (ping -n 1) each machine first and check the return code of the ping.  If it responds than issue the shutdown command.  You could also use WMI win32_pingstatus, if you are familiar with WMI scripting.  If not download scriptomatic (free tool) and give it a try.  It is pretty simple.
0
 

Expert Comment

by:someITguys
Comment Utility
0
 
LVL 27

Expert Comment

by:bluntTony
Comment Utility
How are you scripting this? Are you using WMI to connect and shutdown or another method?
Unfortunately AD doesn't hold info on whether a machine is turned on or not so there's nothing for you to query for this. If you want to speed things up, you might want to consider using a shell object to run shutdown.exe. Using the 'run' method, you can tell the script to just run the command and not wait for it, meaning that it'll carry on and attempt to shut down the next PC.
If you're running on over a 1000 PCs, you may want to get the script to pause after executing x amount of commands, otherwise you could end up with many many shutdown.exe processes running on your machine as each process will timeout for machines that are not on.
A simple example is below. The three arguments for Run are 1. the command to run, 2. Window state (0 means do not show), 3. Wait or not (False means don't wait for the command to complete, just continue). The cn of the machine is effectively it's NetBIOS host name, so you'll require WINS to be functioning (assuming your 1000 machines are spread over different subnets), or you'll need to append the correct DNS suffix to it.

Set sh = CreateObject("Wscript.Shell")

Set objOU = GetObject("LDAP://OU=Computers,OU=MyBusiness,DC=domain, DC=local")

objOU.Filter = Array("Computer")
 

For Each objPC In objOU

	sh.Run "%systemroot%\system32\shutdown.exe -s -f \\" & objPC.cn,0,False

Next

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 25

Expert Comment

by:Ron M
Comment Utility
If you use a CMD script...a batch file....each command will have to complete or timeout before the next command runs.

Using VBscript like what BluntTony has provided will run the shutdown command without waiting...

The alternative would be to put a shutdown scheduled task on each machine... instead of scripting a remote shutdown solution.
http://technet.microsoft.com/en-us/library/cc772785.aspx#BKMK_create
0
 
LVL 27

Expert Comment

by:bluntTony
Comment Utility
This might run quicker - you can capture the output of a quick ping command and if it's a success, then shutdown the machine. It might be better than spawning many shutdown processes that can take a while to time out, considering you have quite a few machines to shut down.
I've appending the DNS suffix to be safe as well (strDNSSuffix)
Tony

strDNSSuffix = ".domain.local"

Set sh = CreateObject("Wscript.Shell")

Set objOU = GetObject("LDAP://OU=Computers,OU=MyBusiness,DC=domain, DC=local")

objOU.Filter = Array("Computer")

 

For Each objPC In objOU

	Set objExec = sh.Exec("%systemroot%\system32\ping -n 2 -w 1000 " & objPC.name & strDNSSuffix) 

	Do Until objExec.Status

		WScript.Sleep 250

	Loop

	If InStr(strOutput,"ttl") > 0 Then sh.Run "%systemroot%\system32\shutdown.exe -s -f \\" & objPC.name & strDNSSuffix,0,False

Next

Open in new window

0
 
LVL 27

Accepted Solution

by:
bluntTony earned 500 total points
Comment Utility
Oops, missed a line out....

strDNSSuffix = ".domain.local"

Set sh = CreateObject("Wscript.Shell")

Set objOU = GetObject("LDAP://OU=Computers,OU=MyBusiness,DC=domain, DC=local")

objOU.Filter = Array("Computer")

 

For Each objPC In objOU

	Set objExec = sh.Exec("%systemroot%\system32\ping -n 2 -w 1000 " & objPC.name & strDNSSuffix) 

	Do Until objExec.Status

		WScript.Sleep 250

	Loop

	strOutput = LCase(objExec.StdOut.ReadAll)

	If InStr(strOutput,"ttl") > 0 Then sh.Run "%systemroot%\system32\shutdown.exe -s -f \\" & objPC.name & strDNSSuffix,0,False

Next

Open in new window

0
 

Author Closing Comment

by:afeind
Comment Utility
Good solution - added some of my own scripting and batch stuff to make it shutdown list of computers attached to server.
0
 

Expert Comment

by:Rahhali
Comment Utility
Hi
From goggling online I know there is an option of creating schedule task under the Group policy management
I have tried the following:

GPO Path: Computer Configuration/Preferences/COntrol Panel Settings/Scheduled Tasks

Create a new Schedule Task.

Settings:

TAB TASKS

name: Scheduled Shutdown Site 1

run: C:\Windows\System32\shutdown.exe

arguments: /s /f /t 45 /c "This is a scheduled shutdown of your computer, please close all programs for the time being"

Set Enabled(scheduled taks runs at a specified time)

TAB SCHEDULE

Set your time settings
0

Featured Post

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!

Join & Write a Comment

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Learn about cloud computing and its benefits for small business owners.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

11 Experts available now in Live!

Get 1:1 Help Now