Solved

Automatically Shutdown of computers in OU

Posted on 2009-04-04
9
1,056 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
 
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 25

Expert Comment

by:Ron M
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

12 Experts available now in Live!

Get 1:1 Help Now