Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Executing batch files in PS scripts

Reply
 
Old 06-27-2007   #1 (permalink)
David


 
 

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
Old 06-28-2007   #2 (permalink)
Marco Shaw


 
 

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
Old 06-28-2007   #3 (permalink)
David


 
 

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
Old 06-28-2007   #4 (permalink)
David


 
 

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
Old 06-28-2007   #5 (permalink)
David


 
 

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
Old 06-28-2007   #6 (permalink)
David


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
executing scripts within a web browser control under the system ac .NET General
Executing PowerShell Scripts with IIS PowerShell
executing powershell scripts on server PowerShell
executing scripts async on remote hosts PowerShell
Executing scripts in the current directory PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46