We help IT Professionals succeed at work.

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

Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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 ...

Author

Commented:
@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

Author

Commented:
@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 ...
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
Of corse!  Thanks tricky :)

Author

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

Thanks and cheers!
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Good luck.

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
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 :-)
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
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

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

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

~bp
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
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

Author

Commented:
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

Author

Commented:
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
IT Consultancy
CERTIFIED EXPERT
Commented:
Rene,

I think you would need to use for the tasklist method I did is either add an * or the full filename, i.e. psexec to the filter:

TASKLIST /FI "IMAGENAME EQ PSEXEC.exe" && GOTO loop

TASKLIST /FI "IMAGENAME EQ PSEXEC*" && GOTO loop

But nothing wrong with your use of tasklist with findstr ... although it would be listing and passing through findstr all the tasks, it is only doing this every 5 seconds, possibly only once...

As to   START "" PSEXEC %%A cmd /c ECHO [%%time%%] %%A > "%LogBase%_!Count!_TMP.log" I haven't got psexec on this pc yet but what combination (if any) brings the output back to the source machine:

PSEXEC \\testpc cmd /c ECHO [%%time%%] \\testpc
START "" PSEXEC \\testpc cmd /c ECHO [%%time%%] \\testpc
(START "" PSEXEC \\testpc cmd /c ECHO [%%time%%] \\testpc) > afile.txt

Will fire up one with psexec etc. on in a bit and see myself.

Steve
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Just for the heck of it, can I suggest an alternate approach?  Rather than spawning a bunch of PSEXEC sessions, and then trying to coordinate the output to pull it back together, why not just use the NET TIME command, and process them serially?  The NET TIME command runs pretty quick, so may come out in a decent place.  Below is a first pass at what this might look like.  The only refinement you might want to add (I know you can handle this part Rene) is to do a quick pink of the computer before executing the NET TIME.  In the case where a PC doesn't respond that would help since the NET TIME command takes 30 or 60 seconds to time out I think.

Not sure what level of accuracy you are looking for in this exercise, but since in either approach not all the servers will respond at exactly the same time, I thought it might be useful to log the time from the executing machine just before getting the time from the remote system, as a point of reference.

@echo off
set "LogFile=%~dpn0.log"
if exist "%LogFile%" del "%LogFile%"
for /F %%A in ('net view ^| findstr "\\"') do (
  echo Checking: %%A
  echo %DATE% %TIME% - Checking: %%A>>"%LogFile%"
  net time %%A | find /I "Current time at">>"%LogFile%" || echo %%A Failed time check.>>"%LogFile%"
)
pause
exit /b

Open in new window

~bp
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Nice Bill, never thought of using net time.

Steve

Author

Commented:
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

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
@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.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Just a side note: PowerShell has great means for job control - asynchronous execution of parallel tasks, collecting the output, aso. So using PowerShell just to execute portqry and net time might be a great opportunity to learn that.

Author

Commented:
@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?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.

Author

Commented:
@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?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
This question went to another direction.
Do you feel it is pointless persuing this, or there could be some hope?
Thanks
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
@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

Author

Commented:
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?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Fine by me.

~bp
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
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 :-)

Author

Commented:
Steve, are you telling me that the points I just gave you made you cross the 1M finish line?
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
yup looks like it according to the email.....  Three good answers in a row after a few empty days :-)  thanks Rene!!
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Woo hoo!

~bp
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
ahh looks like it was the next one here.... But you got me within 700 points along the way:

¿http://www.experts-exchange.com/Microsoft/Development/Q_27451181.html?cid=1131#a37151262

steve

Author

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

Congratulations for your 1M

Explore More ContentExplore courses, solutions, and other research materials related to this topic.