[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Batch file copying

I have a text file with a list of computers that I need to copy a file to but a service needs to be stopped before the copy can take place. I'd like to make a batch file that reads the computer list and one by one stops the service, copies the file and then restarts the service.  This is all on a AD domain with all Windows XP clients.
0
jrluquette
Asked:
jrluquette
1 Solution
 
Steve KnightIT ConsultancyCommented:
You can read the text file with for /f and SC should be able to sort the service out.  ry something like this.  

Yourfile.txt contains the PC list
logfile.txt is created with success / fails in

@echo off
for /f %%a in (yourfile.txt) do call :process %%a
start logfile.txt
goto :eof

:process
 set error=
 echo Working on computer: %1
 sc \\%1 stop "%servicename%"
 if errorlevel 1 set error=Stopping service & goto error
 xcopy yourfile.exe "\\%1\c$\program files\whatever\yourfile.exe" /Y
 if errorlevel 1 set error=Copying File %errorlevel% & goto error
 sc \\%1 start "%servicename%"
 if errorlevel 1 set error=Starting service & goto error
 echo Completed OK %1 >> logfile.txt
goto :eof

:error
 echo Error with %1 (%error%) >> logfile.txt
0
 
VorenusCommented:
You can use psexec for that, to launch a batch file on each computer :

stop the service (net stop servicename)
copy the file located to the server to a location on the computer :
Example : copy \\server\share\file.ext "%windir%\" /Y
and then restart the service (net start servicename)

psexec allows you to load a computer list from a text file and launch the commands on each of those computers.

More details regarding psexec at : http://www.microsoft.com/technet/sysinternals/utilities/psexec.mspx
0
 
SteveGTRCommented:
You could use http://www.microsoft.com/technet/sysinternals/utilities/psexec.mspx to access the remote computers:

@echo off

setlocal

for /f "tokens=*" %%a in ('type COMPUTERS.TXT') do (
  psexec \\%%a net stop Spooler
  copy c:\yourfile\temp.txt \\%%a\c$\dir
  psexec \\%%a net start Spooler
)

You'd want to change the specifics, server (I'm using Spooler), file information in the copy command.
0
Industry Leaders: 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!

 
SteveGTRCommented:
dragon-it's use of the sc command is a much better choice then my psexec usage.
0
 
jrluquetteAuthor Commented:
So here is the file with my edits added to it. There must be something wrong with it because it just flashes on the screen and doesn't do anything on the client end.  Any suggestions?

@echo off
for /f %%a in c:\temp\computers1.txt do call :process %%a
start logfile.txt
goto :eof

:process
 set error=
 echo Working on computer: %1
 sc \\%1 stop "%WOupdateMon%"
 if errorlevel 1 set error=Stopping service & goto error
 xcopy c:\temp\WoupdateMonitor.bat "\\%1\c$\scripts" /Y
 if errorlevel 1 set error=Copying File %errorlevel% & goto error
 sc \\%1 start "%WOupdateMon%"
 if errorlevel 1 set error=Starting service & goto error
 echo Completed OK %1 >> logfile.txt
goto :eof

:error
 echo Error with %1 (%error%) >> logfile.txt
0
 
SteveGTRCommented:
Change your for statement to this:

for /f %%a in (c:\temp\computers1.txt) do call :process %%a

Also, I don't see where %WOupdateMon% is set.
0
 
Steve KnightIT ConsultancyCommented:
I think SteveGTR has spotted it there (sorry was in bed!).

So I'd change your batch too:

@echo off
set servicename=WOupdateMon

REM Step through each line in computers1.txt
REM and call the subroutine for each with %1 = computername
REM
for /f %%a in (c:\temp\computers1.txt) do call :process %%a
start logfile.txt
goto :eof

:process
 set error=
 echo Working on computer: %1
 sc \\%1 stop "%servicename%"
 if errorlevel 1 set error=Stopping service & goto error
 xcopy c:\temp\WoupdateMonitor.bat "\\%1\c$\scripts" /Y
 if errorlevel 1 set error=Copying File %errorlevel% & goto error
 sc \\%1 start "%servicename%"
 if errorlevel 1 set error=Starting service & goto error
 echo Completed OK %1 >> logfile.txt
goto :eof

:error
 echo Error with %1 (%error%) >> logfile.txt
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now