?
Solved

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

Posted on 2006-05-26
7
Medium Priority
?
3,277 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:KaliKoder
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 1000 total points
ID: 16771226
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
 
LVL 11

Author Comment

by:KaliKoder
ID: 16771606
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 1000 total points
ID: 16771656
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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 11

Author Comment

by:KaliKoder
ID: 16771688
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 16771793
@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 16772617
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
 
LVL 11

Author Comment

by:KaliKoder
ID: 16922570
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

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question