Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3660
  • Last Modified:

Batch File SET /P command not working?

I have built this shutdown batch command that I would like to use to schedule server shutdowns.  The problem lies with my set /p command not functioning properly.

When running after running the Set /P I get this message:
ERROR: Invalid Start Date (Date should be in "mm/dd/yyyy" format).

Can someone help?  Is what I'm trying to do possible?  I only want to set the date and time once while I'm in the batch.
@ECHO off
 
REM CODED BY ME
REM VERSION - 02202009
REM 
 
goto menu
 
:menu
cls
cls
set _task=
echo ************************* SCHEDULE SHUTDOWN BATCH *************************
echo *  CHOICE   DESCRIPTION                        CHOICE   DESCRIPTION       *
echo *  0        SET DATE AND TIME                                             *
echo *           --- PHYSICAL ---                            --- VIRTUALS ---  *
echo *  1        SVR-DC1                            vfs2     SVR-FS2           *
echo *  2        SVR-DB1                            v2       SVR-AP2           *
echo *  3        SVR-EX1                            v3       SVR-AP3           *
echo *  4        SVR-BU1                            v4       SVR-AP4           *
echo *  5        SVR-AP10                           v5       SVR-AP5           *
echo *  6        SVR-                               v6       SVR-AP6           *
echo *                                              v7       SVR-AP7           *
echo *                                              v8       SVR-AP8           *
echo *                                              v9       SVR-AP9           *
echo *                                              v11      SVR-AP11          *
echo *                                              vim1     SVR-IM1           *
echo *                                              vrl1     SVR-RL1           *
echo *                                              vav1     SVR-AV1           *
echo *                                                                         *
echo *  ALL       Schedule shutdown of all servers - Caution                   *
echo *  H         Show help                                                    *
echo *  Q         Quit without any action                                      *
echo *                                                                         *
echo *   YOU RUN THIS BAT FILE AT YOUR OWN RISK.  NO WARRANTIES ARE IMPLIED!   *
echo ***************************************************************************
set /p  _task=Please choose a menu option:
if "%_task%" == "ALL" goto all
if "%_task%" == "all" goto all
if "%_task%" == "v11" goto ap11
if "%_task%" == "v9" goto ap9
if "%_task%" == "v8" goto ap8
if "%_task%" == "v7" goto ap7
if "%_task%" == "v6" goto ap6
if "%_task%" == "v5" goto ap5
if "%_task%" == "v4" goto ap4
if "%_task%" == "v3" goto ap3
if "%_task%" == "v2" goto ap2
if "%_task%" == "vav1" goto av1
if "%_task%" == "vrl1" goto rl1
if "%_task%" == "vim1" goto im1
if "%_task%" == "vfs2" goto fs2
if "%_task%" == "6" goto menu
if "%_task%" == "5" goto ap10
if "%_task%" == "4" goto bu1
if "%_task%" == "3" goto ex1
if "%_task%" == "2" goto db1
if "%_task%" == "1" goto dc1
if "%_task%" == "0" goto datetime
If "%_task%" == "Q" goto quit
If "%_task%" == "q" goto quit
if "%_task%" == "H" goto help
if "%_task%" == "h" goto help
 
:help
cls
echo You must set a date and time in order for this to work.
echo You must also run this command from a 2003 server.
echo Do not run from XP as not all the command options are available.
echo.
echo.
echo.
echo ISSUES - PLEASE CONTACT ME - 
echo.
echo YOU RUN THIS BAT FILE AT YOUR OWN RISK.  NO WARRANTIES ARE IMPLIED!
echo.
pause
goto menu
 
:quit
cls
EXIT
 
:datetime
echo date has to be in this format xx/xx/xxxx example is 02/25/2000
set /P 1date1=Set Date of Shutdown:
echo time has to be in this format xx:xx:xx example is 16:00:00 for 4PM
set /P 1time1=Set Time of Shutdown:
GOTO MENU
 
REM ---------------START DC1 CODE------------------------
:dc1
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-dc1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END DC1 CODE--------------------------
REM ---------------START DB1 CODE------------------------
:db1
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-db1 /RU SYSTEM /TR "shutdown /s /t 120 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END DB1 CODE--------------------------
REM ---------------START EX1 CODE------------------------
:ex1
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ex1 /RU SYSTEM /TR "shutdown /s /t 10 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END EX1 CODE--------------------------
REM ---------------START BU1 CODE------------------------
:bu1
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-bu1 /RU SYSTEM /TR "shutdown /s /t 30 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END BU1 CODE--------------------------
REM ---------------START AP10 CODE------------------------
:ap10
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap10 /RU SYSTEM /TR "shutdown /s /t 120 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN
Pause
GOTO MENU
REM ---------------END AP10 CODE--------------------------
 
 
REM ---------------START AP2 CODE------------------------
:ap2
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap2 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP2 CODE--------------------------
REM ---------------START AP3 CODE------------------------
:ap3
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap3 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP3 CODE--------------------------
REM ---------------START AP4 CODE------------------------
:ap4
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap4 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP4 CODE--------------------------
REM ---------------START AP5 CODE------------------------
:ap5
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap5 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP5 CODE--------------------------
REM ---------------START AP6 CODE------------------------
:ap6
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap6 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP6 CODE--------------------------
REM ---------------START AP7 CODE------------------------
:ap7
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap7 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP7 CODE--------------------------
REM ---------------START AP8 CODE------------------------
:ap8
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap8 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP8 CODE--------------------------
REM ---------------START AP9 CODE------------------------
:ap9
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap9 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP9 CODE--------------------------
REM ---------------START AP11 CODE------------------------
:ap11
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-ap11 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AP11 CODE--------------------------
REM ---------------START FS2 CODE------------------------
:fs2
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-fs2 /RU SYSTEM /TR "shutdown /s /t 30 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END FS2 CODE--------------------------
REM ---------------START IM1 CODE------------------------
:im1
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-im1 /RU SYSTEM /TR "shutdown /s /t 15 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END IM1 CODE--------------------------
REM ---------------START RL1 CODE------------------------
:rl1
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-rl1 /RU SYSTEM /TR "shutdown /s /t 15 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END RL1 CODE--------------------------
REM ---------------START AV1 CODE------------------------
:av1
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-av1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
GOTO MENU
REM ---------------END AV1 CODE--------------------------
 
 
REM ---------------START ALL CODE----------------------------------------------------
:all
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMANDS
schtasks /create /S svr-av1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap11 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ex1 /RU SYSTEM /TR "shutdown /s /t 10 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-fs2 /RU SYSTEM /TR "shutdown /s /t 30 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-dc1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-bu1 /RU SYSTEM /TR "shutdown /s /t 30 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-im1 /RU SYSTEM /TR "shutdown /s /t 15 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-rl1 /RU SYSTEM /TR "shutdown /s /t 15 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap2 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap3 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap4 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap5 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap6 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap7 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap8 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap9 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
schtasks /create /S svr-ap10 /RU SYSTEM /TR "shutdown /s /t 120 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN
pause
schtasks /create /S svr-db1 /RU SYSTEM /TR "shutdown /s /t 120 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
pause
GOTO MENU
REM ---------------END ALL CODE------------------------------------------------------

Open in new window

0
lawson2305
Asked:
lawson2305
  • 11
  • 8
  • 6
1 Solution
 
Steve KnightIT ConsultancyCommented:
Well when I've used schtasks it does seem quite finicky in wanting (in my case) dd/mm/yyyy, i.e. 23/02/2009 and will moan if it gets 23/ 2/2009 or 23/2/2009, same goes for time being hh:mm:ss as opposed to hh:mm etc

Are you entering 28/02/2009 in full etc. (or possibly 02/28/2009 if yours does use mm/dd/yyyy?).

You could so some error checking on the date entered using %1date1:~0,2% etc. but that does get pretty messy in batch...

e.g.
if not "%1date1:~2,1%%1date1:~5,1%"=="//" echo Please enter dd/mm/yyyy & goto error
if not 1%1date1:~0,2% GTR 100 echo Please enter day as dd
if not 1%1date1:~3,2% GTR 100 echo Pleae enter month as mm
if not %1date1:~6,4% GTR 2008 echo Please enter year as yyyy

etc.

Steve
0
 
Steve KnightIT ConsultancyCommented:
schtasks /create /? for me shows it wants dd/mm/yyyy btw (though that is my date format am a little surprised it knows of that in the helptext...).  Other people seem to have different date formats... maybe the server you are running it against etc. has a different date format, or default date format set?

0
 
lawson2305Author Commented:
Here is directly from my 2003 server:
[code]
/SD          startdate         Specifies the first date on which the
                                       task runs. The format is mm/dd/yyyy.
[/code]
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Steve KnightIT ConsultancyCommented:
Fair enough.... I can only get it to give that error if entering it as 21/ 2/2009 etc.  Can I suggest running it without th echo off at the top and then you can see the schtasks line where the error occurs and what it is passing to it.  Either that or add the word "echo" before each schtasks command so you can have it display the command rather than running it.... that might give us some clues, even if not obvious if you could post the output.

thanks


Steve
0
 
lawson2305Author Commented:
Sounds good i will test this result and provide details.
FYI I did this weekend do a find and replace on %1date1% and %1time1% and this worked but I would like it more interactive so others can use it.
0
 
Steve KnightIT ConsultancyCommented:
No problem, give it a go.  What you are attempting to do should be fine... so as you say give it a try when you can and post the results... the echo before the schtasks is the easiest potentially as it then it doesn't effect anything (i.e. just search/replace "schtasks" for "echo schtasks"

Steve
0
 
lawson2305Author Commented:
Ok this portion of the command I did a test from my workstation so had to remove the last two switches:
ECHO SCHEDULING SHUTDOWN COMMAND
schtasks /create /S svr-av1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN
Pause

Tested this after setting the date and time in the batch.
Here are my results.

C:\Documents and Settings\2305\Desktop>ECHO SCHEDULING SHUTDOWN COMMAND
SCHEDULING SHUTDOWN COMMAND

C:\Documents and Settings\2305\Desktop>schtasks /create /S svr-av1 /RU SYSTEM
 /TR "shutdown /s /t 60 /f /d p:0:0" /SD date11time1 /SC ONCE /TN ADHOC-SHUTDOWN

ERROR: Invalid Start Date (Date should be in "mm/dd/yyyy" format).
Type "SCHTASKS /CREATE /?" for usage.

C:\Documents and Settings\2305\Desktop>Pause
Press any key to continue . . .
0
 
Steve KnightIT ConsultancyCommented:
ok.... we can see the entries have all got a bit mangled there. Odd.
Can you add please
echo %1date1%
and
echo %1time1% to the line before the schtasks to see what it shows pls?

Could I suggest using something other than a 1 at the start of the variable name please?  This leaves it open to being interpreted as %1 i.e. passed parameter which could be the issue here.

Try changing all %1date1% to %thedate% or something (not %date% obviously as that is in-built).

I think it is interpreting it as

%1 date1 % /ST % 1time1 %.....

i.e. %1 then text date1 then % /ST % then 1time1 then %and the rest of the line...

Hadn't sotted the variable name as it looked like a lowercase l to me before.

thanks

Steve
0
 
lawson2305Author Commented:
Ran set date time and set date to
date=02/01/2009
time=12:00:00
-------------------------------------
ECHO SCHEDULING SHUTDOWN COMMAND
ECHO ON
echo %date%
echo %time%
schtasks /create /S svr-av1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %date% /ST %time% /SC ONCE /TN ADHOC-SHUTDOWN
Pause
------------------------------------------
Looks Good
------------------------------------------
SCHEDULING SHUTDOWN COMMAND

C:\Documents and Settings\2305\Desktop>echo 02/01/2009
02/01/2009

C:\Documents and Settings\2305\Desktop>echo 12:00:00
12:00:00

C:\Documents and Settings\2305\Desktop>schtasks /create /S svr-av1 /RU SYSTEM
 /TR "shutdown /s /t 60 /f /d p:0:0" /SD 02/01/2009 /ST 12:00:00 /SC ONCE /TN AD
HOC-SHUTDOWN

I didn't want to use date and time because if I used these terms elsewhere.
0
 
lawson2305Author Commented:
When doing the first test with it set like this 1date1 and 1time1
----------------------------------------------
:datetime
echo date has to be in this format xx/xx/xxxx example is 02/25/2000
set /P 1date1=Set Date of Shutdown:
echo time has to be in this format xx:xx:xx example is 16:00:00 for 4PM
set /P 1time1=Set Time of Shutdown:
GOTO MENU

cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
ECHO ON
echo %1date1%
echo %1time1%
schtasks /create /S svr-av1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %1date1% /ST %1time1% /SC ONCE /TN ADHOC-SHUTDOWN
Pause
ECHO OFF
GOTO MENU

----------------------------------------------------------------

C:\Documents and Settings\2305\Desktop>echo date1
date1

C:\Documents and Settings\2305\Desktop>echo time1
time1

C:\Documents and Settings\2305\Desktop>schtasks /create /S svr-av1 /RU SYSTEM
 /TR "shutdown /s /t 60 /f /d p:0:0" /SD date11time1 /SC ONCE /TN ADHOC-SHUTDOWN

ERROR: Invalid Start Date (Date should be in "mm/dd/yyyy" format).
Type "SCHTASKS /CREATE /?" for usage.

C:\Documents and Settings\2305\Desktop>Pause
0
 
AmazingTechCommented:
%1 is a variable try changing them to something like

aDate1
aTime1

----------------------------------------------
:datetime
echo date has to be in this format xx/xx/xxxx example is 02/25/2000
set /P adate1=Set Date of Shutdown:
echo time has to be in this format xx:xx:xx example is 16:00:00 for 4PM
set /P atime1=Set Time of Shutdown:
GOTO MENU

cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
ECHO ON
echo %adate1%
echo %atime1%
schtasks /create /S svr-av1 /RU SYSTEM /TR "shutdown /s /t 60 /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN
Pause
ECHO OFF
GOTO MENU

----------------------------------------------------------------
0
 
Steve KnightIT ConsultancyCommented:
Thats what I said :-)  Also said not to use %date% as that is todays date...


Steve
0
 
AmazingTechCommented:
Give this a try.
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION 
REM CODED BY ME
REM VERSION - 02202009
REM 
 
For /l %%a in (1,1,14) do Set LineP%%a=*   	                                       
For /l %%a in (1,1,14) do Set LineV%%a=   	                     
 
Set P=1
for /f "tokens=1,2,3 delims=," %%b in (servershutdown.txt) do (
    IF %%b LEQ 9 (
        Set LineP!p!=*  %%b	    %%c                                           
        Set /a p+=1
    )
)
 
For /l %%a in (1,1,14) do Set LineP%%a=!LineP%%a:~0,35!
 
Set /a V=1
for /f "tokens=1,2,3 delims=," %%b in (servershutdown.txt) do (
    IF %%b gtr 9 (
       Set LineV!V!=%%b	%%c           
       Set /a v+=1
    )
)
 
For /l %%a in (1,1,14) do Set LineV%%a=!LineV%%a:~0,25!	  *
 
goto menu
 
:menu
cls
cls
set _task=
echo ************************* SCHEDULE SHUTDOWN BATCH *************************
echo *  CHOICE   DESCRIPTION               CHOICE    DESCRIPTION               *
echo *  0        SET DATE AND TIME                                             *
echo *           --- PHYSICAL ---                    --- VIRTUALS ---          *
For /l %%a in (1,1,14) do ECHO !LineP%%a!!LineV%%a!
echo *  ALL       Schedule shutdown of all servers - Caution                   *
echo *  H         Show help                                                    *
echo *  Q         Quit without any action                                      *
echo *                                                                         *
echo *   YOU RUN THIS BAT FILE AT YOUR OWN RISK.  NO WARRANTIES ARE IMPLIED    *
echo ***************************************************************************
set /p  _task=Please choose a menu option:
if /i "%_task%" == "ALL" goto all
if "%_task%" == "6" goto menu
if "%_task%" == "0" goto datetime
If /i "%_task%" == "Q" goto quit
if /i "%_task%" == "H" goto help
if /i "%_task%" == "" ECHO Invalid selection && PAUSE && goto Menu
GOTO OneServer
 
:help
cls
echo You must set a date and time in order for this to work.
echo You must also run this command from a 2003 server.
echo Do not run from XP as not all the command options are available.
echo.
echo.
echo.
echo ISSUES - PLEASE CONTACT ME - 
echo.
echo YOU RUN THIS BAT FILE AT YOUR OWN RISK.  NO WARRANTIES ARE IMPLIED!
echo.
pause
goto menu
 
:quit
cls
EXIT
 
:datetime
echo date has to be in this format xx/xx/xxxx example is 02/25/2000
set /P adate1=Set Date of Shutdown:
echo time has to be in this format xx:xx:xx example is 16:00:00 for 4PM
set /P atime1=Set Time of Shutdown:
GOTO MENU
 
REM ---------------START DC1 CODE------------------------
:OneServer
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
for /f "tokens=1,2,3 delims=," %%b in ('findstr /b /c:"%_Task%" servershutdown.txt') do (
    schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
)
 
 
Pause
GOTO MENU
 
 
REM ---------------START ALL CODE----------------------------------------------------
:all
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMANDS
for /f "tokens=1,2,3 delims=," %%b in ('findstr /b /c:"%%a" servershutdown.txt') do (
    schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
)
 
 
pause
GOTO MENU
REM ---------------END ALL CODE------------------------------------------------------

Open in new window

0
 
AmazingTechCommented:
Opps didn't attach the file.
servershutdown.txt
0
 
lawson2305Author Commented:
AmazingTech,

Can you give a brief explanation of what you are doing here?  So I may understand it.
0
 
AmazingTechCommented:
Basically taken all your servers and put them into a file. Added the choice and shutdown timeout to each server separated by a comma.

The batch file reads in servershutdown.txt and creates the menu choices.

Now you can easily add more servers without having to change the code.

I haven't added things like what happens when you make an incorrect choice.

Although right now you have 13 virtual servers but only allocated 14 lines for server list.

I know you didn't ask for this but I just can't help showing more elaborate things you can do with batch files so you're reducing lines and making it easier to add servers.
0
 
lawson2305Author Commented:
I understand and can see how this would be beneficial to saving me time in the future.
0
 
lawson2305Author Commented:
AmazingTech
Is there a way to echo the command that is run using your batch file process?

Basically what I can do if I don't use your file is this:
[code]
ECHO ON
schtasks /create /S svr-ap2 /RU SYSTEM /TR "shutdown /s /t 05 /f /d p:0:0" /SD %zdate% /ST %ztime% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
Pause
[/code]
This then returns:
[code]
C:\Documents and Settings\user\Desktop>schtasks /create /S svr-ap2 /RU SYST
EM /TR "shutdown /s /t 05 /f /d p:0:0" /SD 03/24/2009 /ST 12:30:00 /SC ONCE /TN
ADHOC-SHUTDOWN /Z /F
INFO: The schedule task "ADHOC-SHUTDOWN" will be created under user name ("NT AU
THORITY\SYSTEM").
SUCCESS: The scheduled task "ADHOC-SHUTDOWN" has successfully been created.
[/code]
It is real handy to be able to see the actual code you execute in this situation to make sure it is doing what you want.
0
 
AmazingTechCommented:
Which lines from my code do you want to echo? So do you want it echoed to a file?
0
 
lawson2305Author Commented:
I want to echo the schtasks command.  So I can see what command it is executing.  As I have displayed above.
So in your code I want it to echo this when it is executed:

schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F

Open in new window

0
 
AmazingTechCommented:
OK. So why not echo it before executing it?


echo schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F

Open in new window

0
 
lawson2305Author Commented:
so like this?
Because I did try echo on
before running the command.  Did not work.
for /f "tokens=1,2,3 delims=," %%b in ('findstr /b /c:"%%a" servershutdown.txt') do (
  echo schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F 
)

Open in new window

0
 
AmazingTechCommented:
Yes. That looks about right.  The problem with echo on is there is so much other stuff that runs it's hard to tell what is what. With echo off and just echoing the stuff you want to see is much clearer.
0
 
lawson2305Author Commented:
This does not work.

If I use what I put in then I get this
DID YOU SET DATE AND TIME?
IF NOT PLEASE CLOSE WINDOW AND START OVER
Press any key to continue . . .
SCHEDULING SHUTDOWN COMMAND
Press any key to continue . . .

And no command is run and no task is created.

If I leave it as it was the task is created but I don't see the command executed and looks like this:
DID YOU SET DATE AND TIME?
IF NOT PLEASE CLOSE WINDOW AND START OVER
Press any key to continue . . .
SCHEDULING SHUTDOWN COMMAND
INFO: The schedule task "ADHOC-SHUTDOWN" will be created under user name ("NT AU
THORITY\SYSTEM").
SUCCESS: The scheduled task "ADHOC-SHUTDOWN" has successfully been created.
Press any key to continue . . .
0
 
AmazingTechCommented:
Should be like this.
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION 
REM CODED BY ME
REM VERSION - 02202009
REM 
 
For /l %%a in (1,1,14) do Set LineP%%a=*   	                                       
For /l %%a in (1,1,14) do Set LineV%%a=   	                     
 
Set P=1
for /f "tokens=1,2,3 delims=," %%b in (servershutdown.txt) do (
    IF %%b LEQ 9 (
        Set LineP!p!=*  %%b	    %%c                                           
        Set /a p+=1
    )
)
 
For /l %%a in (1,1,14) do Set LineP%%a=!LineP%%a:~0,35!
 
Set /a V=1
for /f "tokens=1,2,3 delims=," %%b in (servershutdown.txt) do (
    IF %%b gtr 9 (
       Set LineV!V!=%%b	%%c           
       Set /a v+=1
    )
)
 
For /l %%a in (1,1,14) do Set LineV%%a=!LineV%%a:~0,25!	  *
 
goto menu
 
:menu
cls
cls
set _task=
echo ************************* SCHEDULE SHUTDOWN BATCH *************************
echo *  CHOICE   DESCRIPTION               CHOICE    DESCRIPTION               *
echo *  0        SET DATE AND TIME                                             *
echo *           --- PHYSICAL ---                    --- VIRTUALS ---          *
For /l %%a in (1,1,14) do ECHO !LineP%%a!!LineV%%a!
echo *  ALL       Schedule shutdown of all servers - Caution                   *
echo *  H         Show help                                                    *
echo *  Q         Quit without any action                                      *
echo *                                                                         *
echo *   YOU RUN THIS BAT FILE AT YOUR OWN RISK.  NO WARRANTIES ARE IMPLIED    *
echo ***************************************************************************
set /p  _task=Please choose a menu option:
if /i "%_task%" == "ALL" goto all
if "%_task%" == "6" goto menu
if "%_task%" == "0" goto datetime
If /i "%_task%" == "Q" goto quit
if /i "%_task%" == "H" goto help
if /i "%_task%" == "" ECHO Invalid selection && PAUSE && goto Menu
GOTO OneServer
 
:help
cls
echo You must set a date and time in order for this to work.
echo You must also run this command from a 2003 server.
echo Do not run from XP as not all the command options are available.
echo.
echo.
echo.
echo ISSUES - PLEASE CONTACT ME - 
echo.
echo YOU RUN THIS BAT FILE AT YOUR OWN RISK.  NO WARRANTIES ARE IMPLIED!
echo.
pause
goto menu
 
:quit
cls
EXIT
 
:datetime
echo date has to be in this format xx/xx/xxxx example is 02/25/2000
set /P adate1=Set Date of Shutdown:
echo time has to be in this format xx:xx:xx example is 16:00:00 for 4PM
set /P atime1=Set Time of Shutdown:
GOTO MENU
 
REM ---------------START DC1 CODE------------------------
:OneServer
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMAND
for /f "tokens=1,2,3 delims=," %%b in ('findstr /b /c:"%_Task%" servershutdown.txt') do (
    echo schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
    schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
)
 
 
Pause
GOTO MENU
 
 
REM ---------------START ALL CODE----------------------------------------------------
:all
cls
ECHO DID YOU SET DATE AND TIME?
ECHO IF NOT PLEASE CLOSE WINDOW AND START OVER
Pause
ECHO SCHEDULING SHUTDOWN COMMANDS
for /f "tokens=1,2,3 delims=," %%b in ('findstr /b /c:"%%a" servershutdown.txt') do (
    ECHO schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
 
    schtasks /create /S %%c /RU SYSTEM /TR "shutdown /s /t %%d /f /d p:0:0" /SD %adate1% /ST %atime1% /SC ONCE /TN ADHOC-SHUTDOWN /Z /F
)
 
 
pause
GOTO MENU
REM ---------------END ALL CODE------------------------------------------------------

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now