Solved

Automatically Shutdown of computers in OU

Posted on 2009-04-04
9
1,087 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
ID: 24067548
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
ID: 24067550
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
ID: 24067563
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:bluntTony
ID: 24067641
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
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 24080019
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
ID: 24080272
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
ID: 24080287
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
ID: 31566583
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
ID: 39181542
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I guess it is not common knowledge to most Wintel engineers/administrators: If you have an SNMP-based monitoring system in your environment (and it's common to have SNMP or Syslog) it's reasonably easy to enable monitoring of the Windows Event logs,…
Learn about cloud computing and its benefits for small business owners.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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