Link to home
Start Free TrialLog in
Avatar of KaliKoder
KaliKoderFlag for Canada

asked on

Batch file to copy a scheduled task command to a list of computers

Hello,

We have a WIndows XP environment. I have a txt file with one computername per line. What I would like to do is to deploy a scheduled task to each machine thats listed in the text file using the schtask command:

example:

schtasks /create /tn "RebootJob1" /tr \\server1\DailyReboot.bat /ru %COMPUTERNAME%\someuser /rp somepassword"

Thanks!
SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KaliKoder

ASKER

Thanks for your reply Steve.

I can pass the servername just in the command itself, as it would never change. What I need to do, is to deploy three tasks on each machine thats specified in a .txt file.

Can you please let me know how i can do this, basically I believe you are very close. However I need to do something like this:

Start:
For each computer listed in computername.txt
    Do
       {schtasks /create /tn "RebootJob1" /tr \\DataServer\install\DailyReboot.bat /ru %COMPUTERNAME%\Administrator /rp 7876tree"
schtasks /create /tn "RebootJob2" /tr \\Dataserver\install\DailyReboot.bat /ru %COMPUTERNAME%\Administrator /rp 7876tree"
schtasks /create /tn "RebootJob3" /tr \\Dataserver\install\DailyReboot.bat /ru %COMPUTERNAME%\Administrator /rp 7876tree"
}
End.

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Dragon-it, I believe thats what I was after, I can understanf the variable %Infile% because I would pass it with the name of batch file, like "test.bat computer.txt" however, what does %%a do ?

Also, I need to deploy three jobs, not just one, can I have three for statements in the same batch file ? with each deploying one job ?

Thanks again you guys
@echo off

set inFile=%~1

if "%inFile%"=="" set /p inFile=Please enter file name:
if "%inFile%"=="" echo Processing aborted...&goto :EOF
if not exist "%inFile%" echo %inFile% does not exists.&goto :EOF

REM ** Remove echo to enable
for /f "delims=" %%a in ('type "%inFile%"') do (
    echo schtasks /create /tn "RebootJob1" /tr \\%%a\install\DailyReboot.bat /ru %COMPUTERNAME%\Administrator /rp 7876tree"
    echo schtasks /create /tn "RebootJob2" /tr \\%%a\install\DailyReboot.bat /ru %COMPUTERNAME%\Administrator /rp 7876tree"
    echo schtasks /create /tn "RebootJob3" /tr \\%%a\install\DailyReboot.bat /ru %COMPUTERNAME%\Administrator /rp 7876tree"
)

The %%a is the name of the server in the input file. You can change around your schtasks line anyway you need. If you want to hardcode the file name then do the following:

Change:

set inFile=c:\fully qualified\file.txt

Remove:

if "%inFile%"=="" set /p inFile=Please enter file name:
if "%inFile%"=="" echo Processing aborted...&goto :EOF
if not exist "%inFile%" echo %inFile% does not exists.&goto :EOF

Ahh, my earlier post crossed with the asker's first comments BTW

SteveGTR, I think he still needs the paramaters like I suggested, i.e. the /s to specify the servername, otherwise as yours...

echo schtasks /create /s %%a /tn "RebootJob1" /tr \\server\install\DailyReboot.bat /ru %%a\Administrator /rp 7876tree"
echo schtasks /create /s %%a /tn "RebootJob2" /tr \\server\install\DailyReboot.bat /ru %%a\Administrator /rp 7876tree"
echo schtasks /create /s %%a /tn "RebootJob3" /tr \\server\install\DailyReboot.bat /ru %%a\Administrator /rp 7876tree

Infile is set to the value passed on the commandline (%1) and if it is blank then it uses set /p to prompt for you to enter a filename.

The %%a gets set to the pcname on each line in the inputfile one by one so effectively the above line becomes this then the same for each other line in the file.

echo schtasks /create /s pcname1 /tn "RebootJob1" /tr \\server\install\DailyReboot.bat /ru pcname1\Administrator /rp 7876tree"
echo schtasks /create /s pcname1 /tn "RebootJob2" /tr \\server\install\DailyReboot.bat /ru pcname1\Administrator /rp 7876tree"
echo schtasks /create /s pcname1 /tn "RebootJob3" /tr \\server\install\DailyReboot.bat /ru pcname1\Administrator /rp 7876tree"

hope that helps

Steve
Thanks guys, I am sorry I forgot to get back to you. Both of you provided detailed and to the point help in resolving my issue.