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
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.txt. 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!