Run PsExec non-sequentially in a batch file

Discussion in 'Scripting' started by M, Mar 25, 2010.

  1. M

    M Guest


    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

    Thank you.
    M, Mar 25, 2010
    1. Advertisements

  2. 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

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

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

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

    if exist c:\Server3.txt del c:\Server3.txt
    cmd.exe /c psexec \\Server3 ipconfig /all > c:\Server3.txt
    Pegasus [MVP], Mar 25, 2010
    1. Advertisements

  3. M

    Al Dunbar Guest

    What might not be clear in the above solution is an explanation of the

    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 ^>

    Al Dunbar, Mar 26, 2010
  4. M

    M Guest

    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.
    M, Mar 26, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.