Batch file: Run specific part on particular OS - batch-file

I'm trying to create a script with the purpose of creating a scheduled task on either a W2K3 or W2K12 server (more to be added later on) depending on the target server. I won't create separate scripts for each server type as this is already a part of a bundle of installation scripts that need to be distributed via a single package.
There are different users for the servers.
I have tried the following, but both jobs are created on each server type, with the one being redundant as it does not fit. I only want one scheduled task to be created dependent on the server type.
I'm a bit blind for the moment on how to solve it, also it would be great to avoid using "goto". I would appreciate your take on it.
setlocal
set runlevel=
for /f "tokens=2*" %%i in ('reg.exe query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentVersion"') do set os_ver=%%j
if /i "%os_ver:~,1%" EQU "5.2" (
set runlevel=/rl HIGHEST goto W2K3
exit
)
else
(
if /i "%os_ver:~,1%" GEQ "6.2" (
set runlevel=/rl HIGHEST goto W2K12
exit
)
)
:W2K3
schtasks.exe /create /tn "Files Handler W2K3" /sc DAILY /TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /ru User1 /rp epicfun %runlevel%
:w2K12
schtasks.exe /create /tn "Files Handler W2K12" /sc DAILY /TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /ru User2 /rp newkidontheblock %runlevel%

Here's an idea using wmic instead of the registry, to get your required version number. This additionally filters the results to only those with a product type of 3, (Server).
#SetLocal EnableExtensions
#Set "strVer="
#For /F "EOL=V Tokens=1-2 Delims=." %%G In (
'%__AppDir__%wbem\WMIC.exe OS Where "ProductType='3'" Get Version'
) Do #Set /A "strVer=%%G%%H" 2>NUL
#If Not Defined strVer GoTo :EOF
#If %strVer% Equ 52 (%__AppDir__%schtasks.exe /Create /RU User1 /RP epicfun^
/SC DAILY /TN "Files Handler W2K3" /TR "D:\TMP_DONT_DELETE\Files_Handler.bat"^
/ST 05:30) Else If %strver% Equ 62 %__AppDir__%schtasks.exe /Create /RU User2^
/RP newkidontheblock /SC DAILY /TN "Files Handler W2K12"^
/TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /RL HIGHEST
I have removed the /RL option from the Windows 2003 SchTasks command because Microsoft says it isn't available in that OS, feel free to put it back in, if you don't believe them.

Put an goto :EOF statement after the :W2K3 schtasks.exe statement and remove %runlevel% because it's not supported as noted in Compo's answer:
...
:W2K3
schtasks.exe /create /tn "Files Handler W2K3" /sc DAILY /TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /ru User1 /rp epicfun
goto :EOF
...

Related

IF exist for scheduled task

I have created a batch file to check if scheduled task exists and if they don't create them, however, my if exist rule seem to always hit true even though the jobs are not there.
Any ideas?
::Check Rule
IF EXIST SchTasks /QUERY /TN "Cache Task Morning" (
echo ! Morning rule in place!
GOTO NEXT
) ELSE IF NOT EXIST SchTasks /Create /SC DAILY /TN "Cache Task Morning" /TR "C:\Cache Clear\Cache Clear.bat" /ST 09:00
:NEXT
IF EXIST SchTasks /QUERY /TN "Cache Task Afternoon" (
echo ! Afternoon rule in place!
GOTO NEXT TWO
) ELSE IF NOT EXIST SchTasks /Create /SC DAILY /TN "Cache Task Afternoon" /TR "C:\Cache Clear\Cache Clear.bat" /ST 15:00
:NEXT TWO
IF EXIST SchTasks /QUERY /TN "Cache Task Evening" (
echo ! Evening rule in place!
GOTO CLEAR CACHE
) ELSE IF NOT EXIST SchTasks /Create /SC DAILY /TN "Cache Task Evening" /TR "C:\Cache Clear\Cache Clear.bat" /ST 18:00
You cannot use if exist with schtasks, this is not the purpose of if exist.
use either
schtasks /query /TN "task_name" >NUL 2>&1 || schTasks /Create /SC DAILY /TN "task_name" ...
or
schtasks /query /TN "task_name" >NUL 2>&1
if %errorlevel% NEQ 0 schTasks /Create /SC DAILY /TN "task_name" ...
>NUL 2>&1 suppress success or error output, but we have errorlevel set to 0 if success, or not 0 if failure.
Both are pretty the same. In the first case we use the cmd && and || operators, where && means previous command succesful and || means previous command failed.
as in
mycommand && echo success || echo error
As we only want to test failure, then use only || (previous command failed).

Oracle service auto start bach file

I have 6 Java services and 1 Oracle services running on my application server. Oracle service stops when RAM is full. I need a batch file to run automatically when the Oracle service stops.
But this batch file should be checked continuously. But I do not know how the batch file was created.
Windows Server Task Manager:
You can create a task that triggers upon event, but config the event to trigger when the service stops is not easy.
Another point of view
You can set an scheduled task to run a batch script to test if the service is running. If it isn't, it will start it.
#echo off
SetLocal EnableDelayedExpansion
set "user=username"
set "pass=p#ssword"
set "host=server_name"
set "OracleServiceTime=3"
set "OracleServiceTask=CheckOracle"
set "OracleServiceName=the_name_of_the_service"
rem schtasks /query /TN "%OracleServiceTask%" /S "%host%" >NUL 2>&1
schtasks /query /TN "%OracleServiceTask%" >NUL 2>&1
if %ERRORLEVEL% EQU 1 (
rem schtasks /create /SC MINUTE /MO %OracleServiceTime% /TN "%OracleServiceTask%" /RL HIGHEST /TR "%~dpnx0" /S "%host%" /RU "%user%" /RP "%pass%" /F >NUL 2>&1
schtasks /create /SC MINUTE /MO %OracleServiceTime% /TN "%OracleServiceTask%" /RL HIGHEST /TR "%~dpnx0" /F >NUL 2>&1
if !ERRORLEVEL! NEQ 0 (
rem if task couldn't be created, message user or whatever.
)
)
tasklist /FI "imagename eq %OracleServiceName%" | find /I "RUNNING" > NUL 2>&1
if %ERRORLEVEL% EQU 0 goto :EOF
rem sc "\\%host%" start "%OracleServiceName%" > NUL 2>&1
sc start "%OracleServiceName%" > NUL 2>&1
if %ERRORLEVEL% NEQ 0 (
rem if service couldn't be started, message user or whatever.
)
EndLocal
exit/B
This script check if the task exist, if it doesn't then create it (in this case, to run every 3 minutes)
Then test if the service is running and, if not, start it.
There are two command lines for schtask /create and sc start. If the script may run on remote system you'll need the host user pass syntax.

How can I make the find function not display anything on the command line?

Here's what I got:
SCHTASKS /query /tn "test" | find /c "Running" &&(
goto finish
)||(
SCHTASKS /create /tn "test" /sc minute /mo 10 /tr c:\bin\go.vbs
)
:finish
echo.
echo Done
echo.
timeout 3
I want to check to see if the test task is already running. The functionality of this works, but even with echo off, the find /c part keeps returning either a 1 or a 0, depending on if the task was running or not.
Is there a way I can make it so that it isn't printing out a 1 or a 0 whenever I run it in the command line?
Ended up doing this:
SCHTASKS /query | findstr /n "test" &&(
goto finish
)||(
SCHTASKS /create /tn "test" /sc minute /mo 10 /tr c:\bin\go.vbs
)
:finish
echo.
echo Done
echo.
timeout 3
| find /c "Running">Nul 2>&1&&(
Make your change as above.
Your script may be better like this too:
SCHTASKS /query /tn "test" | find /c "Running">Nul 2>&1&&(goto :finish)
SCHTASKS /create /tn "test" /sc minute /mo 10 /tr c:\bin\go.vbs
:finish
echo.
echo Done
echo.
timeout 3

Batch script to get the time of file when it arrived at given location

i have a automated JOB that will look inside a folder for some specific file.I need to create a Batch Script that will look inside that folder and search those files which arrived at a certain time. If files are there in the folder which arrived at that point of time then it will move those files at different location.
Thanks
Prakhar
Rather you need SCHTASKS :
SCHTASKS /create /tn "move files" /tr "cmd /c move /y \"c:\file1\" \"c:\other_dir\file1"" /sc daily /sd 01/01/2015 /st 10:00
you can export the command to a separate bat and schedule its call"
SCHTASKS /create /tn "move files" /tr "cmd /c c:\dir\some.bat" /sc daily /sd 01/01/2015 /st 10:00
Where the some.bat content is like:
#echo off
move /y "c:\dir1\file1" "c:\other_dir\file1"

How do I schedule a task at a random time via batch file?

I need to create a script that will run via a .bat file on Windows 7 machines.
The batch file will create a scheduled task that runs at a random time between 11am and 9pm on Sunday.
I am currently using this command that I know works but only at a specific time:
schtasks /create /tn "task" /tr c:\task.vbs /sc WEEKLY /d SUN /st 21:00:00 /ru ""
I want to randomize the time, so that I can run the same exact batch file across all 400 systems and have each of them get a different time within that range, so they aren't running the task at the same time. I don't care if a few of them get identical times, but I don't want all 400 running within the same few minutes.
Once I have a single batch file that works as intended, it would be pushed out to all systems and run one time to schedule that task.
Based on searching other answers here, I've tried using the following scripts, for now just trying to generate a random hour, but I am not getting the results I need:
Option 1)
set /a num=%random% * (21 - 11 + 1) / 32768 + 11
That command will generate a number within the correct range, but it's not really random, it seems to be based on the clock or something, because if I run it several times real quick, I get the same number and then it increments up as time passes, this wont work.
Option 2)
#echo off & setlocal EnableDelayedExpansion
for /L %%a in (1 1 20) do (
call:rand 11 21
echo !RAND_NUM!
) >> random2.txt
goto:EOF
REM The script ends at the above goto:EOF. The following are functions.
REM rand()
REM Input: %1 is min, %2 is max.
REM Output: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF
That code, generates the random number correctly, but I don't know how to insert that number into the command like I want. I have tried replacing the ">> random2.txt" with the schtasks command, but it doesn't work, and I'm not getting and feed back to a log file to see why, I've tried it like this:
#echo off & setlocal EnableDelayedExpansion
for /L %%a in (1 1 1) do (
call:rand 11 21
echo !RAND_NUM!
)
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st %RANDOM%:00:00 /ru "" > task.txt 2>&1
goto:EOF
REM The script ends at the above goto:EOF. The following are functions.
REM rand()
REM Input: %1 is min, %2 is max.
REM Output: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF
So in that I've tried to use the %RANDOM% var as the hour component of the command.
The perfect solution would generate a random two digit number between 11 and 21 for the hours section and then a two digit number between 00 and 59 for the minutes section of the command with a colon in between them as required by the format of the command
so the time section of the schtasks command would look something like this:
%hour11-21%:%min00-59%:00
I hope I have given enough detail here, can anyone tell me what am I doing wrong? Is there an easier way to do this?
Thank you in advance!
Try this:
#echo off
setlocal enabledelayedexpansion
call :rand 1 1 dummy
call :rand 11 21 ret
call :rand 0 59 ret2
if %ret2% LSS 10 set ret2=0%ret2%
echo schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st %ret%:%ret2%:00 /ru "" > task.txt 2>&1
exit /b
:rand
setlocal
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
endlocal & set %~3=%RAND_NUM%
exit /b
Output:
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 11:00:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 16:7:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 17:46:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 19:35:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 13:54:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 14:51:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 21:48:00 /ru ""
If you schedule the task with schtasks and want the start time to vary every time the task is run, you can use this in a batch file:
:: Delay for up to 5 minutes (300 seconds)
set /A DELAY=%RANDOM% %% 301
ping /n %DELAY% /w 1000 localhost > NUL
You could use timeout instead of ping on Windows 7/Server 2008+.
I solved it with:
set /a H=%random% * (21 - 11 + 1) / 32768 + 11
set /a M=%random% * (59 - 11 + 1) / 32768 + 11
H = Hour
M = Minute
then the scheduled task created with
SCHTASKS /Create /RU "NT AUTHORITY\SYSTEM" /SC WEEKLY /D FR /ST %H%:%M%
Greets,
Dan.
Way too complex set /a commands. Use % = "remainder" which works like "modulo" for positive values. Even if it is counting "up" by only one digit in the short time, avoiding / 32768 makes it more random since I had that seemingly "not as random as I thought" effect too.
You need to use double % when using modulo in a .CMD, therefore we end up with triple %
:hour 11:00 until 21:00
set /a HOUR=%RANDOM%%%11+11
:minute 0-59
set /a MINUTE=%RANDOM%%%60
schtasks /create /tn "task" /tr c:\task.vbs /sc WEEKLY /d SUN /st %HOUR%:%MINUTE%:00 /ru ""

Resources