0

Currently working on a script to ping every host on a /24 subnet, and then executes another script which runs psexec on those machines which are online. The ping sweep script is called ping.bat and the other script which actually runs psexec on the machines is called deploy_mir.bat. I can simply run deploy_mir.bat on a remote host and it will run no problem.

The problem im having is that every time mir.bat runs, which itself contains a loop, it will display the help info for psexec in the cmd window. As far as i can tell everything is working fine, aside from the annoying fact that everytime the loop inside of mir.bat runs my cmd window gets filled with the help info for psexec. I dont have @echo enabled, not that it would cause this anyway.

hoping for a quick fix, but if my code is needed to get an answer ill post it.

Posting the code anyway...

@echo on

setlocal EnableDelayedExpansion

set /p ipAddress="enter ip address: "

for /l %%i in (1,1,255) do (
ping -n 1 %ipAddress%.%%i | find "TTL" > nul

if !errorlevel! == 0 (
call deploy_mir.bat %ipAddress%.%%i
)
)

endlocal

deploy_mir.bat code

@ECHO OFF

echo "Mir Agent deployment to: %1"


rem net use T: \\%1\C$ /user:administrator "password"
net use T: \\%1\C$ /user:administrator "username"

copy /y conf.xml T:\WINDOWS\
copy /y setup_mir.bat T:\WINDOWS\
net use t: /delete

rem psexec \\%1 -i -u administrator -p "password" c:\windows\setup_mir.bat
psexec \\%1 -i -u administrator -p "username" c:\windows\setup_mir.bat

Desired cmd line result of running deploy_mir.bat

C:\DOCUME~1\socuser2\MIR>deploy_mir.bat 10.180.145.66
"Mir Agent deployment to: 10.180.145.66"
The command completed successfully.

        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
t: was deleted successfully.


PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com


c:\windows\setup_mir.bat exited on 10.180.145.66 with error code 0.

C:\DOCUME~1\socuser2\MIR>
user1336749
  • 15
  • 1
  • 8
  • At risk of sounding obvious, I would take another look at your code. As you probably know, help info usually only comes up when something is wrong with your syntax or you've asked for it with a /? or something. What does your code look like in the ping batch? – iesou Apr 30 '12 at 13:49
  • deploy_mir.bat is the only script that uses psexec, and it was created by someone else and works no problem. My script mir.bat only calls deploy_mir.bat. – user1336749 Apr 30 '12 at 14:08
  • also, i added the code from mir.bat to my question – user1336749 Apr 30 '12 at 14:28
  • When I answered [your previous question](http://stackoverflow.com/a/10322053/591064), I used a deploy_mir.bat that had a signle line : `@echo %*`. No matter who wrote deploy_mir.bat, show us the code. Show us a command line that produces the output you are looking for. If you can modify it, put an `echo` in front of psexec to see the command line used. With that in hand, a fix is mere seconds away. – ixe013 Apr 30 '12 at 17:11
  • i posted the code for deploy_mir.bat in my original question – user1336749 Apr 30 '12 at 17:40
  • as well as the desired results of running it – user1336749 Apr 30 '12 at 17:48
  • There is a copy/paste error somewhere: you have 2 copy operations but three lines in the output. Does not matter if that's the only difference. – ixe013 Apr 30 '12 at 19:01

2 Answers2

1

Just a suggestion. Not sure if it will solve your problem, but may provide some guidance:

My first step would be a small test by explicitly calling psexec on some test batch file in place of the line call deploy_mir.bat %ipAddress%.%%i. If no help message appears, since deploy_mir.bat works find on its own, try explicitly placing it's content in place of the same line call deploy_mir.bat %ipAddress%.%%i. If that works, then there is some issue in the said line we've been replacing. I believe dos / batch will open a sub shell from this line of code and run it's code in that scope. That may be causing the problem. Just guessing with the information provided.

Code Specific Notes:

@echo is enabled, but you say it is not in your question. !errorlevel! == 0 should be !errorlevel! EQU 0

Some General Notes:

In general, I used to pass parameters to batch scripts in quotes, then strip the quotes with %~1 once inside the batch script. Similarly for if conditions, as !someVar! == a will throw and error if someVar is not set / empty, while "!someVar!" == "a" will gracefully not meet the criteria of the if condition.

kikuchiyo
  • 3,391
  • 3
  • 23
  • 29
0

I don't know why it works when called from outside the loop. But the psexec line in deploy_mir.bat should have cmd /c.

psexec \\%1 -i -u administrator -p "username" cmd /c c:\windows\setup_mir.bat
ixe013
  • 9,559
  • 3
  • 46
  • 77
  • i dont think thats necessary, like ive been saying 'deploy_mir.bat' works no problem at all. – user1336749 May 01 '12 at 11:39
  • It took more typing to tell that an idea is not good than to try it out... I suggest you look around and get the feel of how Stackoverflow works – ixe013 May 01 '12 at 12:07
  • do you understand what im saying when i say that deploy_mir.bat is not the issue in this whole thing?? The issue is somewhere in mir.bat, i can run deploy_mir.bat on its own and it works. When i try to execute deploy_mir.bat from inside mir.bat, thats when we have problems. Logic would deduce that the issue is in mir.bat – user1336749 May 01 '12 at 12:22
  • I'm just doing my best, for free, to help you with the information we have. I'm didn't mean to waste your time. [This method](http://mattgemmell.com/2008/12/08/what-have-you-tried/) is a long read, but it is often quoted on SO as being helpful. I'm done, but adding an echo before psexec and posting the results of the failed launch will help you find the difference between the two runs. – ixe013 May 01 '12 at 13:19