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!
LVL 11
KaliKoderAsked:
Who is Participating?
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
I presume slight change SteveGTR in that it is the %computername% bit that needs to be different :-)

for /f "delims=" %%a in ('type "%inFile%"') do echo schtasks /create /tn "RebootJob1" /tr \\Server1\share\DailyReboot.bat /ru %%a\someuser /rp somepassword"  

but not sure how that is scheudling it to run it on the remote computer.  You need to use /S etc?

for /f "delims=" %%a in ('type "%inFile%"') do echo schtasks /create /s %%a /tn "RebootJob1" /tr \\server\share\DailyReboot.bat /ru %%a\somelocaluser /rp somepassword

plus some scheduling parts to the command too I guess.

Steve
0
 
SteveGTRConnect With a Mentor Commented:
Could try this. The batch file will accept a file name with the server on the command line. If no parameter is specified it will prompt for the file. I've echoed the command. To enable remove the echo:

@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\DailyReboot.bat /ru %COMPUTERNAME%\someuser /rp somepassword"  

Good Luck,
Steve
0
 
KaliKoderAuthor Commented:
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
KaliKoderAuthor Commented:
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
0
 
SteveGTRCommented:
@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

0
 
Steve KnightIT ConsultancyCommented:
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
0
 
KaliKoderAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.