Solved

Xcopy Batch file, report back success or failure of hostnames on LAN

Posted on 2010-08-20
32
1,187 Views
Last Modified: 2012-05-10
Hello Experts,

I am trying to automate a process by using the XCOPY command to multiple computers.  I can get the xcopy to work, but I would like to have the command report back the hostnames in which the copy was successful on, and the same for failures?
It only seems to report back my local computer name which I am running the script from.  
I am running this script locally on a LAN and don’t have access to GP.
Any help is greatly appreciated!

So far this is what I have:

REM *** Copy’s AO folder with file content needed to do administrative functions***

@echo off

FOR /f %%x in (computertest.txt) DO XCOPY  “c:\AO\*"  "\\%%x\c$\AO\"  /y

Echo hostname > \\66BQ1k1\c$\Logs\%computername%

Pause
0
Comment
Question by:SgtofMarines
  • 13
  • 12
  • 7
32 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 33487099
I have not tested this.
REM *** Copy’s AO folder with file content needed to do administrative functions***



@echo off



FOR /f %%x in (computertest.txt) DO XCOPY  “c:\AO\*"  "\\%%x\c$\AO\"  /y



if exist "\\%%x\c$\AO\" then

Echo hostname > \\66BQ1k1\c$\Logs\%computername% success

else

Echo hostname > \\66BQ1k1\c$\Logs\%computername% failure



Pause

Open in new window

0
 

Author Comment

by:SgtofMarines
ID: 33487374
Thanks for your quick response, unfortunately it did not work, here is the output which failed:

C:\PStools>REM ::::Copys AO folder w
 functions::::::
0 File(s) copied
0 File(s) copied
0 File(s) copied
'else' is not recognized as an inter
operable program or batch file.
Press any key to continue . . .

It wrote my local computer name to the log folder as well.

I know its just something minor that needs to be adjusted
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33488186
I made some changes for it to work on my system(s) that I can change back if you need me to but this is the code I came up with and the file attached is the results.txt after I ran it. I only had the one computer in computertest.txt.
REM *** Copy’s AO folder with file content needed to do administrative functions***



@echo off



setlocal EnableExtensions EnableDelayedExpansion



echo > c:\Logs\results.txt



FOR /f %%x in (computertest.txt) DO (



XCOPY "c:\AO\*" "\\%%x\c$\AO\" /y



if exist "\\%%x\c$\AO\" (Echo "%%x success" >> c:\Logs\results.txt) else (echo "%%x failure" >> c:\Logs\results.txt)



)



Pause

Open in new window

results.txt
0
 

Author Comment

by:SgtofMarines
ID: 33488443
Hi, this definetely got me closer, the results are outputting now.  The only thing its not doing, is its not copying the AO folder over the the hosts local C: drive, which is weird, cause it cleary is defined in the script.  The Results.txt file reads this:

ECHO is off.
"IRM-BOS-02 failure"
"27MVNJ1 failure"
"3ZTVZ61 failure"

I also physically checked via \\netbios name.  

I really appreciate your help thus far.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33488747
There appears to be a permissions issue on the machines administrative share then.

I added a couple of locations to my computertest.txt that I know I had access to and my results were:

ECHO is on.
"LENOVO-0c784a28 failure"                                       No access - it didn't work
"iib-apps\home\sscarnah success"                           Actual share so it created a folder called c$ and placed the folder AO inside that folder - it sort of worked
"iib-apps success"                                                     Have access to administrative share - it worked
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33488755
This is what I have in my computertest.txt to get the results I recieved.:

LENOVO-0c784a28
iib-apps\home\sscarnah
iib-apps
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33490107
Give this a try.

~bp
@echo off
REM *** Copy’s AO folder with file content needed to do administrative functions***
 
setlocal EnableDelayedExpansion
if exist "\\66BQ1k1\C$\Logs\%computername%" del "\\66BQ1k1\C$\Logs\%computername%"
 
for /F %%X in (computertest.txt) do (
  xcopy "C:\AO\*" "\\%%X\C$\AO\" /Y && set Status=Success || set Status=Failure
  echo %%X = !Status!>"\\66BQ1k1\C$\Logs\%computername%"
)
 
pause

Open in new window

0
 

Author Comment

by:SgtofMarines
ID: 33500673
Hi Pony,

If I script up a basic xcopy command line, it goes through fine, so I dont think it would be a permission issue?  I just think it has to do with some of the script parameters, it has to be something minor which needs an adjustment.

Billprew:  Your script did not work unfortunately.

Thanks guys! I know we are close, I have been trying on my end as well.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33500764
==> Billprew:  Your script did not work unfortunately.

Can you provide any additional information to help debug it?

~bp
0
 

Author Comment

by:SgtofMarines
ID: 33501112
HI Bill, sorry about not providing the details, after running the script you proposed, I get this following content error:

0 File(s) copied
0 File(s) copied
0 File(s) copied
Press any key to continue . . .

after I hit any key to continue, the command box goes away and nothing happens.

0
 
LVL 26

Expert Comment

by:pony10us
ID: 33501497
Are these computers all on a domain? The one that fails for me does not have, or rather has a disabled computer account on the domain. The two that passed were on a server (the same server)
0
 

Author Comment

by:SgtofMarines
ID: 33501648
Yes, all computers are on a domain, XP Professional, windows firewalls are turned off.  The 3 computers are powered on and I can access them via a run prompt (\\%computername%)....
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33501894
I just tested by adding a computer account that is NOT disabled on my domain (IBMMJNZ447) and the these are the new results:

ECHO is on.
"LENOVO-0c784a28 failure"
"10.2.4.202 failure"
"iib-apps\home\sscarnah success"
"iib-apps success"
"IBMMJNZ447 success"

The first two are the computer with the disabled account.

Can you REM the @ECHO OFF and look for errors?
REM *** Copy’s AO folder with file content needed to do administrative functions***



rem @echo off



setlocal EnableExtensions EnableDelayedExpansion



echo > c:\Logs\results.txt



FOR /f %%x in (computertest.txt) DO (



XCOPY "c:\AO\*" "\\%%x\c$\AO\" /y



if exist "\\%%x\c$\AO\" (Echo "%%x success" >> \\66BQ1k1c$\Logs\results.txt) else (echo "%%x failure" >> \\66BQ1k1\c$\Logs\results.txt)



)



Pause

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
ID: 33501919
Line 13 needs to be changed. I missed a "\"

if exist "\\%%x\c$\AO\" (Echo "%%x success" >> \\66BQ1k1c$\Logs\results.txt) else (echo "%%x failure" >> \\66BQ1k1\c$\Logs\results.txt)

Should be:

if exist "\\%%x\c$\AO\" (Echo "%%x success" >> \\66BQ1k1\c$\Logs\results.txt) else (echo "%%x failure" >> \\66BQ1k1\c$\Logs\results.txt)

0
 

Author Comment

by:SgtofMarines
ID: 33502181
HI Pony10us,

I performed that change you said, made some progress as the bactch file ran, but this was the output below.  

Nothing was copied, and the log file says failure for all pc's.  Please see below:


C:\PStools>REM *** CopyÆs AO folder with file content needed to do administrativ
e functions***

C:\PStools>rem @echo off

C:\PStools>setlocal EnableExtensions EnableDelayedExpansion

C:\PStools>echo  1>c:\Logs\results.txt

C:\PStools>FOR /F %x in (computertest.txt) DO (
XCOPY "c:\AO\*" "\\%x\c$\AO\" /y
 if exist "\\%x\c$\AO\" (Echo "%x success"  1>>\\66BQ1k1\c$\Logs\results.txt )
else (echo "%x failure"  1>>\\66BQ1k1\c$\Logs\results.txt )
)

C:\PStools>(
XCOPY "c:\AO\*" "\\IRM-BOS-02\c$\AO\" /y
 if exist "\\IRM-BOS-02\c$\AO\" (Echo "IRM-BOS-02 success"  1>>\\66BQ1k1\c$\Logs
\results.txt )  else (echo "IRM-BOS-02 failure"  1>>\\66BQ1k1\c$\Logs\results.tx
t )
)
Invalid path
0 File(s) copied

C:\PStools>(
XCOPY "c:\AO\*" "\\27MVNJ1\c$\AO\" /y
 if exist "\\27MVNJ1\c$\AO\" (Echo "27MVNJ1 success"  1>>\\66BQ1k1\c$\Logs\resul
ts.txt )  else (echo "27MVNJ1 failure"  1>>\\66BQ1k1\c$\Logs\results.txt )
)
Invalid path
0 File(s) copied

C:\PStools>(
XCOPY "c:\AO\*" "\\D8jb9zb1\c$\AO\" /y
 if exist "\\D8jb9zb1\c$\AO\" (Echo "D8jb9zb1 success"  1>>\\66BQ1k1\c$\Logs\res
ults.txt )  else (echo "D8jb9zb1 failure"  1>>\\66BQ1k1\c$\Logs\results.txt )
)
Invalid path
0 File(s) copied

C:\PStools>pause
Press any key to continue . . .
0
 
LVL 26

Accepted Solution

by:
pony10us earned 500 total points
ID: 33502281
invaled path?

One of these is incorrect
   c:\AO\   (The location of the files you want copied)
   \\27MVNJ1\c$\AO\   (Where you are placing the files - This will be created if it doesn't exist)
   \\66BQ1k1\c$\Logs  (Where the results.txt is placed - obiviously this one works)

Can you verify the first one?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:SgtofMarines
ID: 33502409
HI, it worked!  I seen what the issue was, I had the c:\ao present, but there was nothing inside the folder lol.   Once I copied my files I needed in the folder, it worked!

Thanks for your help!
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33502428
Great.  Sometimes we just overlook the simple things like my missing the "\"   :)

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33503365
==> HI, it worked!  I seen what the issue was, I had the c:\ao present, but there was nothing inside
==> the folder lol.   Once I copied my files I needed in the folder, it worked!

Which is likely the same reason your test of my script didn't copy any files...

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33503388
And, FWIW, I think my script will detect a broader range of errors than my friend pony10us, which I think just checks if the parent directory will be created.  You might want to improve that slightly to allow better error trapping...

~bp
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33503529
I agree with bill.  I have been looking at his script and while primarily the same as mine he does have some nice improvements.  Mine only looks for the directory after the xcopy and doesn't really look at anything else.  I believe that his script may even run a bit faster than mine but I haven't tested it.

You might want to give bill's one more try now that you discovered the "missing files" issue and if it works consider splitting the points at the very least.
0
 

Author Comment

by:SgtofMarines
ID: 33504414
sure thing, I will try Bills today, if not, tomorrow and report back the status.
0
 

Author Comment

by:SgtofMarines
ID: 33504526
Hi BIll, I tried your script and it works, however, it does not report back the status of the host(s) success or fail,  It only reported back my local computer name "66BQ1K1" in my logs folder.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33506644
That's rather odd, I reran a test I had created here, which has slightly different folder and share names etc, and it seemed to work okay.  Did you have an "=" after the computer name in the log, or not?

~bp
0
 

Author Comment

by:SgtofMarines
ID: 33509992
Hi, I do have a = sign after the computer name in the log file, it shows one "computer name = success".  The other 2 computers arent logged, but the folder was copied to them.
66BQ1K1
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33510087
Ahhh, that helps.  I see the problem.  Change the following line:

  echo %%X = !Status!>"\\66BQ1k1\C$\Logs\%computername%"

to :

  echo %%X = !Status!>>"\\66BQ1k1\C$\Logs\%computername%"

~bp
0
 

Author Comment

by:SgtofMarines
ID: 33510598
That worked!  what does the double >> do? opposed to the single >?  

Thank you very much, I will give u half the points
0
 

Author Comment

by:SgtofMarines
ID: 33510693
Now, could you please tell me how to give you half the points? I looked around, but no luck in doing so.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33511199
A basic explanation of the difference between > and >> is that you use the > to write to a new file or to overwrite an existing file.  The >> apends to the bottom of an existing file.  

One feature of bill's script is to delete any existing log file at the beginning. If you want to keep the logs for any reason you may wish to add some code to rename/move them. Just a thought.

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33511227
Don't worry about the points, I think you would need to request the question to be reopened by a moderator, and then reclose it with different point assignment, but it's not a big deal.  Just wanted you to get the most robust solution you could, not worried about the points here.

As far as the difference, the > and >> signs are used whenever you want to take the output of a command that would normally display to the screen by default, and redirect it to a file instead.  A single > says every time that line executes, overwrite the entire file with the output of that command.  On the other hand, two >> says to append to the end of whatever is already in the file.

So if I do this:

del test.txt
echo Line1>test.txt
echo Line2>test.txt
echo Line3>test.txt

and then do

type line.txt

I will only see the last line, since the last echo will overwrite the entire file with that output.  If I do the following though:

del test.txt
echo Line1>>test.txt
echo Line2>>test.txt
echo Line3>>test.txt

and then do

type line.txt

I will see all three lines of output.

Hope this helps,
~bp
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33511356
bill:  Thank you bill.  I really don't mind sharing though. And that is a much more indepth and clear explanation of the difference between ">" and ">>" than I gave.

SgtofMarines:   By the way, was I correct about bill's code running faster than mine?  I haven't had time to test it yet and was curious.  I wasn't real happy with the speed when I was testing my code.
0
 

Author Comment

by:SgtofMarines
ID: 33511593
Yeah, it seemed to run a bit more quicker.  I appreciate both of your help.

I have opened up a new question, if you can help, please feel free.

The question is in regards to running a batch file which reports back the usernames logged in and PC names back to me in a text file.  This would have to run on my LAN or WAN.

reward is 250
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now