Discussion:
Run PsExec non-sequentially in a batch file
(too old to reply)
M
2010-03-25 14:14:12 UTC
Permalink
Hello:

Is it possible to run PsExec non-sequentially in a batch file? The example
below does not work. I get the output txt files, but they're blank. If I
remove "start" from each line, then everything works fine, but then the
commands run sequentially, so if I have 30 lines, I'd have to wait several
minutes for the entire batch file to complete. Note that I'm only using
ipconfig as a simple example.

Start C:\PsExec\psexec \\Server1 ipconfig /all > c:\temp\Server1.txt
Start C:\PsExec\psexec \\Server2 ipconfig /all > c:\temp\Server2.txt
Start C:\PsExec\psexec \\Server3 ipconfig /all > c:\temp\Server3.txt
Pause

Thank you.
--
Regards,
M
MCTS, MCSA
http://SysAdmin-E.com
Pegasus [MVP]
2010-03-25 19:21:43 UTC
Permalink
Post by M
Is it possible to run PsExec non-sequentially in a batch file? The example
below does not work. I get the output txt files, but they're blank. If I
remove "start" from each line, then everything works fine, but then the
commands run sequentially, so if I have 30 lines, I'd have to wait several
minutes for the entire batch file to complete. Note that I'm only using
ipconfig as a simple example.
Start C:\PsExec\psexec \\Server1 ipconfig /all > c:\temp\Server1.txt
Start C:\PsExec\psexec \\Server2 ipconfig /all > c:\temp\Server2.txt
Start C:\PsExec\psexec \\Server3 ipconfig /all > c:\temp\Server3.txt
Pause
Thank you.
--
Regards,
M
MCTS, MCSA
http://SysAdmin-E.com
This is tricky stuff. You probably have to do it as below. Make sure to
store the code in the referenced batch file (c:\Tools\PSBatch.bat) and to
invoke it without any parameter.

@echo off
goto Label%1

:Label
set batch=c:\Tools\PSBatch.bat
Start /b %batch% 1
Start /b %batch% 2
Start /b %batch% 3
exit

:Label1
if exist c:\Server1.txt del c:\Server1.txt
cmd.exe /c psexec \\Server1 ipconfig /all > c:\Server1.txt
exit

:Label2
if exist c:\Server2.txt del c:\Server2.txt
cmd.exe /c psexec \\Server2 ipconfig /all > c:\Server2.txt
exit

:Label2
if exist c:\Server3.txt del c:\Server3.txt
cmd.exe /c psexec \\Server3 ipconfig /all > c:\Server3.txt
exit
Al Dunbar
2010-03-26 00:51:22 UTC
Permalink
Post by Pegasus [MVP]
Post by M
Is it possible to run PsExec non-sequentially in a batch file? The
example below does not work. I get the output txt files, but they're
blank. If I remove "start" from each line, then everything works fine,
but then the commands run sequentially, so if I have 30 lines, I'd have
to wait several minutes for the entire batch file to complete. Note that
I'm only using ipconfig as a simple example.
Start C:\PsExec\psexec \\Server1 ipconfig /all > c:\temp\Server1.txt
Start C:\PsExec\psexec \\Server2 ipconfig /all > c:\temp\Server2.txt
Start C:\PsExec\psexec \\Server3 ipconfig /all > c:\temp\Server3.txt
Pause
Thank you.
--
Regards,
M
MCTS, MCSA
http://SysAdmin-E.com
This is tricky stuff. You probably have to do it as below. Make sure to
store the code in the referenced batch file (c:\Tools\PSBatch.bat) and to
invoke it without any parameter.
@echo off
goto Label%1
:Label
set batch=c:\Tools\PSBatch.bat
Start /b %batch% 1
Start /b %batch% 2
Start /b %batch% 3
exit
:Label1
if exist c:\Server1.txt del c:\Server1.txt
cmd.exe /c psexec \\Server1 ipconfig /all > c:\Server1.txt
exit
:Label2
if exist c:\Server2.txt del c:\Server2.txt
cmd.exe /c psexec \\Server2 ipconfig /all > c:\Server2.txt
exit
:Label2
if exist c:\Server3.txt del c:\Server3.txt
cmd.exe /c psexec \\Server3 ipconfig /all > c:\Server3.txt
exit
What might not be clear in the above solution is an explanation of the
phenomenon.

The start command runs the command it is given in a separate process. The
start command itself produces no output, which is why the .txt files are
empty. If you run the start commands without redirecting the output, you
should see that, indeed, no output is done in the console window. If the
command being started opens another console window (as should be the case
for psexec), that is where the output is displayed.

The trick Pegasus provided is to get start to run a controlling batch file
in which the batch file you want the output from is run with redirection. I
don't have an environment in which to test using psexec, but I was able to
use start to run a batch file and capture its output. Here is the syntax:

start cmd.exe /c z.bat ^>z.txt

If this were to work with psexec it might just be a matter of changing this:

Start C:\PsExec\psexec \\Server1 ipconfig /all > c:\temp\Server1.txt

to this:

Start cmd.exe /c C:\PsExec\psexec \\Server1 ipconfig /all ^>
c:\temp\Server1.txt

/Al
M
2010-03-26 13:11:03 UTC
Permalink
Thanks everyone for the replies. I ended up not needing to do anything of
this since plans changed and I won't need to use PsExec now. I will keep
everything for reference and will test it out when I have a chance.
--
Regards,
M
MCTS, MCSA
http://SysAdmin-E.com
Post by Al Dunbar
Post by Pegasus [MVP]
Post by M
Is it possible to run PsExec non-sequentially in a batch file? The
example below does not work. I get the output txt files, but they're
blank. If I remove "start" from each line, then everything works fine,
but then the commands run sequentially, so if I have 30 lines, I'd have
to wait several minutes for the entire batch file to complete. Note that
I'm only using ipconfig as a simple example.
Start C:\PsExec\psexec \\Server1 ipconfig /all > c:\temp\Server1.txt
Start C:\PsExec\psexec \\Server2 ipconfig /all > c:\temp\Server2.txt
Start C:\PsExec\psexec \\Server3 ipconfig /all > c:\temp\Server3.txt
Pause
Thank you.
--
Regards,
M
MCTS, MCSA
http://SysAdmin-E.com
This is tricky stuff. You probably have to do it as below. Make sure to
store the code in the referenced batch file (c:\Tools\PSBatch.bat) and to
invoke it without any parameter.
@echo off
goto Label%1
:Label
set batch=c:\Tools\PSBatch.bat
Start /b %batch% 1
Start /b %batch% 2
Start /b %batch% 3
exit
:Label1
if exist c:\Server1.txt del c:\Server1.txt
cmd.exe /c psexec \\Server1 ipconfig /all > c:\Server1.txt
exit
:Label2
if exist c:\Server2.txt del c:\Server2.txt
cmd.exe /c psexec \\Server2 ipconfig /all > c:\Server2.txt
exit
:Label2
if exist c:\Server3.txt del c:\Server3.txt
cmd.exe /c psexec \\Server3 ipconfig /all > c:\Server3.txt
exit
What might not be clear in the above solution is an explanation of the
phenomenon.
The start command runs the command it is given in a separate process. The
start command itself produces no output, which is why the .txt files are
empty. If you run the start commands without redirecting the output, you
should see that, indeed, no output is done in the console window. If the
command being started opens another console window (as should be the case
for psexec), that is where the output is displayed.
The trick Pegasus provided is to get start to run a controlling batch file
in which the batch file you want the output from is run with redirection.
I don't have an environment in which to test using psexec, but I was able
to use start to run a batch file and capture its output. Here is the
start cmd.exe /c z.bat ^>z.txt
Start C:\PsExec\psexec \\Server1 ipconfig /all >
c:\temp\Server1.txt
Start cmd.exe /c C:\PsExec\psexec \\Server1 ipconfig /all ^>
c:\temp\Server1.txt
/Al
Loading...