Windows Vista Forums

Executing batch files in PS scripts

  1. #1


    David Guest

    Executing batch files in PS scripts

    I am have a trouble writing a PowerShell wrapper around some batch files. I
    have a wrapper working using WScript/VBScript but I would like to move to
    PowerShell.

    The working WScript/VBScript code echos 'y' into a make.bat prompt. CODE 123
    VER 1.2.3 are parameters for make.bat and I'm redirecting stderr to stdout.
    Stdout is dumped into a log file (and monitored for errors, code not shown).

    set oExec = oShell.Exec("%comspec% /c echo y | make.bat CODE 123 VER 1.2.3
    2>&1")
    Do Until oExec.StdOut.AtEndOfStream
    sReadLine = oExec.StdOut.ReadLine()
    fLog.Writeline sReadLine
    loop

    I can get the make batch file to run and pipe 'y' into it, but can't get the
    parameters passed into it.



    echo y | make.bat "CODE 123 VER 1.2.3 2>&1" >build.log

    Is there a PowerShell method of implementing the

    'Do Until oExec.StdOut.AtEndOfStream
    sReadLine = oExec.StdOut.ReadLine()'

    functionality?

    TIA

      My System SpecsSystem Spec

  2. #2


    Marco Shaw Guest

    Re: Executing batch files in PS scripts


    > Is there a PowerShell method of implementing the
    >
    > 'Do Until oExec.StdOut.AtEndOfStream
    > sReadLine = oExec.StdOut.ReadLine()'
    >
    > functionality?


    Don't have the time to try out the DOS part right now.

    You're looking to just capture what's going on like a logfile?

    Might start-transcript/stop-transcript work for you?

    Marco

      My System SpecsSystem Spec

  3. #3


    David Guest

    Re: Executing batch files in PS scripts

    Thanks for the suggestion Marco. I think I need to use the Tee-Object cmdlet
    to pipe the output to a file and process the stream for errors.

    "Marco Shaw" wrote:

    >
    > > Is there a PowerShell method of implementing the
    > >
    > > 'Do Until oExec.StdOut.AtEndOfStream
    > > sReadLine = oExec.StdOut.ReadLine()'
    > >
    > > functionality?

    >
    > Don't have the time to try out the DOS part right now.
    >
    > You're looking to just capture what's going on like a logfile?
    >
    > Might start-transcript/stop-transcript work for you?
    >
    > Marco
    >


      My System SpecsSystem Spec

  4. #4


    David Guest

    RE: Executing batch files in PS scripts



    "David" wrote:

    > I can get the make batch file to run and pipe 'y' into it, but can't get the
    > parameters passed into it.
    >
    > echo y | make.bat "CODE 123 VER 1.2.3 2>&1" >build.log
    >


    I've solved part of this problem by not quoting the parameters for make.bat
    and specifing the path for the make.bat

    echo y | ./make.bat CODE 123 VER 1.2.3 2>&1 >build.log

      My System SpecsSystem Spec

  5. #5


    David Guest

    RE: Executing batch files in PS scripts

    > "y" | ./make.bat CODE 123 VER 1.2.3 2>&1 >build.log

    This appears to function properly but whenever make.bat outputs to stderr,
    powershell outputs the following after make.bat's warning messages;

    At C:\PowerShell\make.ps1:22 char:20
    + "y" | ./make.bat <<<< CODE 123 VER 1.2.3 2>&1 >build.log

    It looks like powershell is trying to parse the commandline, but make.bat is
    using the commandline parameters properly.

      My System SpecsSystem Spec

  6. #6


    David Guest

    RE: Executing batch files in PS scripts

    I couldn't get PowerShell to directly process the batch file without parsing
    errors with the batch file parameters. I was able to pass the command line to
    cmd.exe without errors.

    cmd /c "echo y | makeall.bat CODE 123 VER 1.2.3 2>&1" > "build.log"

    Further processing of the output checking for success by piping the output
    into a function looking for a string.

    function TestSuccess ($success)
    {
    while ($input.movenext())
    {
    if ($input.Current -match $success)
    {
    $global:Complete = $true
    }
    }
    }
    $global:Complete=$false
    cmd /c "echo y | makeall.bat CODE 123 VER 1.2.3 2>&1" | Tee-Object
    "build.log" | TestSuccess "MAKE successfully completed."

      My System SpecsSystem Spec

Executing batch files in PS scripts

Similar Threads
Thread Thread Starter Forum Replies Last Post
executing scripts within a web browser control under the system ac QSIDeveloper .NET General 0 08 Apr 2009
Executing PowerShell Scripts with IIS officer07 PowerShell 2 21 Nov 2008
executing powershell scripts on server how to display variable value? PowerShell 2 03 Oct 2008
executing scripts async on remote hosts Frank PowerShell 1 06 Apr 2007
Executing scripts in the current directory Andrew Watt [MVP] PowerShell 4 15 Nov 2006