Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

Batch File : Run at the same time

Hi there,

I need to have this batch file all the psexec in the FOR loop at the same time.

Of corse, the FOR loop is sequestial, but at least, will not wait that a PSEXEC command finnishes it's journey before running the next PSEXEC.

Thanks for your help,
Rene

@ECHO OFF
SET LogFile="%~dpn0.log"
IF EXIST "%LogFile%" DEL "%LogFile%"
FOR /F %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
  ECHO Checking: %%A
  PSEXEC %%A cmd /c echo %%time%% %%A>>"%LogFile%"
)
PAUSE
EXIT

Open in new window

Avatar of Bill Prew
Bill Prew

The simple answer is likely START, as in:

@ECHO OFF
SET LogFile="%~dpn0.log"
IF EXIST "%LogFile%" DEL "%LogFile%"
FOR /F %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
  ECHO Checking: %%A
  START PSEXEC %%A cmd /c echo %%time%% %%A>>"%LogFile%"
)
PAUSE
EXIT

Open in new window

however, with a large number of these running at the same time, the output log file may not come out the way you want, there could be lines from each PSEXEC intermixed with each other.  Give it a try and see if that happens, if so we may have to work that.

~bp
Put the word start before the command.
To run them in the background add a /b

So your line should read:
Start psexec %%A ...
Avatar of ReneGe

ASKER

@BillPrew
You were right, they all want to access to the file at the same time. So I modified it. See below. However, SET does not work

@tricky98: Thanks for the /b

Cheers
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET LogFile="%~dpn0.log"
SET Count=0
IF EXIST "%LogFile%" DEL "%LogFile%"
FOR /F %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
  SET /a Count+=1
  ECHO Checking: %%A
  START PSEXEC %%A cmd /b /c SET Val.!Count!=%%time%% %%A
)

FOR /F "tokens=2 delims==" %%A IN ('SET Val.') DO (
   ECHO %%A
   ECHO %%A>>"%LogFile%"
)

PAUSE
EXIT

Open in new window

Avatar of ReneGe

ASKER

@tricky98:
FYI: /b does not work the way I did it in the my last version.
The /b comes after the start so
Start /b psexec ...
ReneGe

Unfortunately the SET of the Val. variables will run int the command processor on the remote PC, not the local machine.  So you won't have access to those outside of that CMD session.

You may have to save each servers output to a different log file, and then concatenate them all together at the end.  With wildcards though that can be done with one line.

~bp
Avatar of ReneGe

ASKER

Of corse!  Thanks tricky :)
Avatar of ReneGe

ASKER

@billprew:
I guess I'll have to do it that way. Go da go! Will update this weekend.

Thanks and cheers!
Good luck.

~bp
Maybe something like this:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET LogBase=%~dpn0
SET Count=0

DEL /Q "%LogBase%_*._log"

FOR /F %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
  SET /a Count+=1
  ECHO Checking: %%A
  START PSEXEC %%A cmd /b /c ECHO %%A = %%time%%>"%LogBase%_!Count!._log"
)

COPY "%LogBase%_*._log" "%LogBase%.log"
DEL /Q "%LogBase%_*._log"

PAUSE
EXIT

Open in new window

~bp
sound good there.  only thing i'd say Bill is surely you'd have to wait until all the windows closed before concatenating the logs, e.g. a check with tasklist to make no psexec exist still?

@rene - can you stop posting questions during uk night time... How am I ever going to get to that 1 milliom mark befoe Bill :-)
i.e something like this addition to Bill's script perhaps:

or I suppose just..

ECHO "Please press any key when all PSEXEC windows have closed..."
PAUSE


@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET LogBase=%~dpn0
SET Count=0

DEL /Q "%LogBase%_*._log"

FOR /F %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
  SET /a Count+=1
  ECHO Checking: %%A
  START PSEXEC %%A cmd /b /c ECHO %%A = %%time%%>"%LogBase%_!Count!._log"
)

:loop
  echo Waiting 5 seconds for PSEXEC to finish...
  PING -n 5 127.0.0.1 >NUL 2>&1
TASKLIST /FI "IMAGENAME EQ PSEXEC" && GOTO loop

COPY "%LogBase%_*._log" "%LogBase%.log"
DEL /Q "%LogBase%_*._log"

PAUSE
EXIT

Open in new window

Good point Steve, thankfully your rested brain in the morning keeps my tired brain in the evening honest.

~bp
I don't think we need the individual log files. A single echo command should be executed atomic, so the resulting line should not get distorted. That would also remove the requirement to wait for completion of all psexec processes.
Right, that's where I Was originally, but then I thought Rene said he saw problems. However, rereading, I'm not sure if he has actually tested and seen a problem, or just thought it could be.  I guess once he tests the "simple" approach we can see if that works okay or not.

~bp
Avatar of ReneGe

ASKER

I'm back!

@Steve
C:posting questions during uk night time
R:Working on it! :)

@Qlemo:
C:I don't think we need the individual log
R:What do you mean by "executed atomic"

@billprew:
C:37088699:but then I thought Rene said he saw problems
R:I did run it and got an error saying that the file was used by another process

I will review Bill and Steve latest script right after fooood!

Thanks guys
Avatar of ReneGe

ASKER

Bill script did not work and I did not put time in investigating du to Steve's comment (37087123) and Bill's acknowldgment (37087637).

@Bill:
On your last script, got an error saying that the file was used by another process. I assume it is about the command lines after the FOR loop, as suggested by Steve.

@Steve:
-Tasklist command line did not work. Changed it to : TASKLIST | findstr -i psexec && GOTO loop


@Steve & Bill
-START PSEXEC %%A cmd /b /c ECHO %%A = %%time%%>"%LogBase%_!Count!._log" does not output the infos >"%LogBase%_!Count!._log". However, the files are created, but empty.

WORKS WELL:
COPY "%LogBase%_*._log" "%LogBase%.log"
DEL /Q "%LogBase%_*._log"

Here is the updated code.

Thanks and cheers,
Rene


@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET LogBase=%~dpn0
SET Count=0

DEL /Q "%LogBase%_*._log"

FOR /F %%A IN ('NET VIEW ^| FINDSTR -i "\\pce-admin"') DO (
  SET /a Count+=1
  ECHO Checking: %%A
  START "" PSEXEC %%A cmd /c ECHO [%%time%%] %%A > "%LogBase%_!Count!_TMP.log"
)

:loop
ECHO Waiting 5 seconds for PSEXEC to finish...
PING -n 5 127.0.0.1 >NUL 2>&1
TASKLIST | findstr -i psexec >NUL 2>&1 && GOTO loop
COPY "%LogBase%_*_TMP.log" "%LogBase%.log"
DEL /Q "%LogBase%_*_TMP.log"
PAUSE
EXIT

Open in new window

Avatar of ReneGe

ASKER

I tried the following on line 11, and the copy on line 18 worked correcrly.
ECHO [!Count!] %%A >"%LogBase%_!Count!_TMP.log"

Now, the only issue left to resolve, is that nothing is ouput to "%LogBase%_!Count!_TMP.log".

Thanks again for your help.

Cheers,
Rene
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice Bill, never thought of using net time.

Steve
Avatar of ReneGe

ASKER

Net Time seems to run faster. I wonder if I can run it with the START CMD /C...

Here, I deal with either french and english PCs, so I replaced "current time at" by the PC name.

Also, to prevent sending the command to other network devices like printers..., I verify if port 135 responds.

I earlyer tried to run it by using WMIC to have them running at the same time, and did not work.

Let's see if, with "Net Time", I can run them all at the (kind of) same time.

Go a go. Will update later.

PORTQRY -n !Target! -e 135 | FINDSTR /I /C:"not listening" >NUL || net time %%A | find /I "%%A">>"%LogFile%" || echo %%A Failed time check.>>"%LogFile%"

Open in new window

How many computers do you need to scan, I'd stay away from starting many NET TIME executions, because then you're back to appending all their log files.

Is there really a need to have this complete "fast"?

And what are you dong with the resultant data?

~bp
Avatar of ReneGe

ASKER

@Bill
-Like arround 50 (40 LAN and 10 WAN through VPN)
-I don't need it to be that fast. I always wanted to do a script that does this kind of parallel processing and I found this may be the opportunity.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

@Q:
Hmmmmm, you have a point!!

Plz tell me something, I will soon require to learn DotNet. Do you think it would make sense I skip Powershell and go streight to .Net?
I say it this way, .Net is really most applicable to true programming, in the Microsoft Visual Studio family.  Powershell can leverage .Net when needed, but is also a new and very powerful scripting language for Windows (much like the "shell" flavors in unix).  I think they are used to solve different problems, it's unlikely you would write a full .Net executable for most of the small scripty things we deal with in these EE zones.  But they can compliment each other, I think someone that understands .Net can use PS quite effectively.

~bp
Programming in .Net (C++, C#, VB, ..., with .Net extension) is completely different from writing a batch file.
VBScript is reduced in its capabilities to allow for simpler scripts, but uses some of the .Net features.
PowerShell is somewhere in-between, but can be compared to VB.Net. It is more the question of preference - do you want to have a project with different modules, compiled, together with the resources, aso. I agree to Bill's point of view.
Avatar of ReneGe

ASKER

@bp & Q:
So in other words, on a scal of "1 to 10", where 10 is the most favorable; learning Powershell would make sense as a step up to what I currently know towards .Net?
Yes, I think it could be seen as in the right direction.  Certainly learning PS won't teach you .Net, but the object oriented architecture of PS does transfer I think.  And some of the scripts that you might reference or create in PS could be leveraging .Net capabilities, so there's some knowledge there that could be useful.

But the specific syntax is very different, PS is an actual language, where .Net isn't, it's more of a framework that a number of Microsoft languages embrace.

~bp
Avatar of ReneGe

ASKER

This question went to another direction.
Do you feel it is pointless persuing this, or there could be some hope?
Thanks
@ReneGe

==> This question went to another direction.
==> Do you feel it is pointless persuing this, or there could be some hope?

Sorry Rene, I don't understand what you are asking now?

~bp
Avatar of ReneGe

ASKER

I just wanted to have your feedback as if I should persue this project.
I feel like closing this one by splitting points, and consolidating it to another question.
How does that sound?
Fine by me.

~bp
MS DOS: 1,001,214 (8,998,786 more points until Savant)

Think I'll pass pm the Savant.... thanks for the points there Rene :-)
Avatar of ReneGe

ASKER

Steve, are you telling me that the points I just gave you made you cross the 1M finish line?
yup looks like it according to the email.....  Three good answers in a row after a few empty days :-)  thanks Rene!!
Woo hoo!

~bp
Avatar of ReneGe

ASKER

I wanted to be the one giving you the finish line points!!!

Congratulations for your 1M