• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1213
  • Last Modified:

Automatically Shutdown of computers in OU

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
afeind
Asked:
afeind
1 Solution
 
halejr1Commented:
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
 
someITguysCommented:
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
 
someITguysCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bluntTonyCommented:
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
 
Ron MalmsteadInformation Services ManagerCommented:
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
 
bluntTonyCommented:
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
 
bluntTonyCommented:
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
 
afeindAuthor Commented:
Good solution - added some of my own scripting and batch stuff to make it shutdown list of computers attached to server.
0
 
RahhaliCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now