Automating Computer Lab Shutdown

Automating Computer Lab Shutdown

We've been challenged to automate the shutdown of multiple labs of computers. My first thought was to use the Power Control in DS with a reoccurring job. The only problem here is that if the computer is already off, the DS job will turn on the computer to then shut it off, which for me does not make much sense. My second thought was to use Windows initiate the shutdown command through remote script execution of a batch file. One of the important keys to know is the switches for the shutdown command:

Syntax
Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]
No args 
Display this message (same as -?)
-i
Display GUI interface, must be the first option
-l
Log off (cannot be used with -m option)
-s
Shutdown the computer
-r
Shutdown and restart the computer
-a
Abort a system shutdown
-m \\computername
Remote computer to shutdown/restart/abort
-t xx
Set timeout for shutdown to xx seconds
-c "comment"
Shutdown comment (maximum of 127 characters)
-f
Forces running applications to close without warning
-d [u] [p]:xx:yy
The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer less than 256)
yy is the minor reason code (positive integer less than 65536)

After scripting a batch file as such...
shutdown -s -m \\computername1
shutdown -s -m \\computername2
shutdown -s -m \\computername3
etc ...

Caution! Do not try this on a Windows Server 2000. Previous to XP the command shutdown does not have the capability to control remote computer and will automatically shutdown the server you work off which caused me a couple of headache until I figure out the problem.

The only problem with this method of running a batch script is that the file hangs when the script reaches a computer that is not on. Thus this is also not a viable solution.

Lastly, I did a little Web searching and came across a batch script that pings all the computers in a subnet to track whether they are off or on. This script can be found at: http://www.ericphelps.com/batch/samples/add.bat.tx.... This got me onto the idea that I could run a loop that would check to see if the computer was on and then -- if it was -- I could send that computer the shutdown command. I do understand that this same process could be scripted through VB but I am not an experienced VB programmer.

This script will loop through a lab full of computers with names such as computer-01, computer-02, etc. In Lab 1 and computer2-01, computer2-02, etc in Lab2. Here is the scipt that I came up with:

REM Lab 1
:START
REM this is the call to another batch file that does the incrementing of the number from 1 to 99
call C:\filepath\add.bat
ping -n 3 computer-%T%%D% | find "Reply" > nul
 if not errorlevel 1 (
	shutdown -s -m \\computer-%T%%D%
	echo Shutting Down computer-%T%%D%)
 if errorlevel 1 echo computer-%T%%D% did not Reply
REM 21 is the number of computers in Lab 1
 if %T%%D%==21 goto NEXT
goto START
:NEXT
REM Lab 2
set T=
set D=
:START2
call C:\filepath\add.bat
ping computer2-%T%%D% | find "Reply" > nul
 if not errorlevel 1 (
	shutdown -s -m \\computer2-%T%%D%
	echo Shutting Down computer2-%T%%D%)
 if errorlevel 1 echo computer2-%T%%D% did not Reply
REM 15 is the number of computers in Lab 2
 if %T%%D%==15 goto NEXT2
goto START2
:NEXT2
:DONE 

This script can be extended to an infinte number labs with computers that have incremental numbering.

You will also need a file in the same directory call add.bat with the scripting below:
:: ADD.BAT
:: Increments a three digit number
:: Works by comparing each digit
:: H=hundreds, T=tens, D=digits
@echo off
if [%H%]==[] set H=0
if [%T%]==[] set T=0
if [%D%]==[] set D=0
:DIGITS
if %D%==9 goto TENS
if %D%==8 set D=9
if %D%==7 set D=8
if %D%==6 set D=7
if %D%==5 set D=6
if %D%==4 set D=5
if %D%==3 set D=4
if %D%==2 set D=3
if %D%==1 set D=2
if %D%==0 set D=1
goto DONE
:TENS
set D=0
if %T%==9 goto HUNDREDS
if %T%==8 set T=9
if %T%==7 set T=8
if %T%==6 set T=7
if %T%==5 set T=6
if %T%==4 set T=5
if %T%==3 set T=4
if %T%==2 set T=3
if %T%==1 set T=2
if %T%==0 set T=1
goto DONE
:HUNDREDS
set T=0
if %H%==9 set H=0
if %H%==8 set H=9
if %H%==7 set H=8
if %H%==6 set H=7
if %H%==5 set H=6
if %H%==4 set H=5
if %H%==3 set H=4
if %H%==2 set H=3
if %H%==1 set H=2
if %H%==0 set H=1
goto DONE
:DONE

The last step is to make a DS reoccurring job that calls this script once per day. I'm sure this could also be scripted in VB and if anyone would like to share, I would love to see it. If anyone has another solution to this challenge, I would love to see that also!

3.682925
Average: 3.7 (41 votes)

Another option

CondorMan's picture

Try creating a "run script" task with the following:

REM Shut Down
shutdown -s -m \\%COMPNAME%

For the "Script Run Location", choose "Locally on the Deployment Server" and disable (uncheck) the option "Run when the agent is connected".

You should then be able to run this on any computer or group of computers you want to shut down, and since it does not need the agent to be connected in order to run, it shouldn't wake up the computer.

Thanks!

Thanks CondorMan... I knew that there should have been an easier way but just couldn't figure out how. Nice Tip!

Also...

REM Shut Down
shutdown -s -m \\%COMPNAME%

Just a thought. If there is a computer that is already off, I believe that the script should still hang whether or not you uncheck "Run when the agent is connected." The reason is that the local server will still try to run the remote shutdown for that computers that is off and when it can't it will hang the script at that point.

However...

CondorMan's picture

It will hang on those clients that are powered off, but since it will run each shutdown command seperately, it will also be simultaneously powering off the computers that are on. The powered off computers will eventually time out, I think it takes about a minute.

However, I just realised that this method requires that you run the server-side script as a domain account. In Deployment Server 6.8 SP2 you can configure the job as a client-side script to assign credentials and switch it to a server-side script and it will keep and use the credentials, but this does not work in 6.9.

Shutdown computers

After following your direction on creating the shutdown script it solve my problem with with computer waking up to be shutdown, but I get a error message in the deployment console that said, "error 6750632 during script excution"

Does anyone know how to solve this error message?

Task Server?

What about using the Task Server to schedule a shutdown at the desired time.

You will have to rollout the Client Task Agent and the Power Management Task Agent from the Configuration tab to your lab machines first though.

Yet Another Option

We had the same challenge (shutting down computers to save energy). I used a batch file, a desktop shortcut, and Task Scheduler to accomplish this.

Batch File
shutdown.exe -s -f -t 180 -c "If you do not want to do this click the StopShutDown Icon on your desktop"

Desktop Shortcut that Cancels Shutdown
Target is C:\WINDOWS\system32\shutdown.exe -a
is copied to All Users Desktop.
This lets users halt the auto shutdown if they happen to be working late.

Script to add scheduled task to Task Scheduler
schtasks /create /tn "Daily_shutdown" /tr c:\scripts\shutdown.bat /sc daily /st 22:30:00 /ed 12/31/2015 /ru "system"
We shut them down at 10:30 PM.

I created Altiris Jobs to manage the addition/deletion of the files and running scripts. I added a registry entry to show it as an installed application for checking if it was installed. I think that's about it. Works great for our needs. Oh, yeah, and it only runs when the computer is on

psshutdown

We did something similar in the past. Instead of using the regular shutdown, we've used psshutdown from SysInternals/Microsoft which is much more flexible. (And it's free, too!)

You can automate all sort of things with it, even force machines down that won't shutdown normally.

Cheers,

Pete.

NS anyone?

You could also set up a SWD job in NS and specify to only run at the specified time. Have the job run a batch file with just a REM in it and select the option to shut down the machine after running.

I used this method in the past and created a Wise script that would create/edit a log file and log the time that the script ran so I would know when the machine was shutdown by the job.

Syndicate content