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 - function return values, console output

Reply
 
Old 08-21-2006   #1 (permalink)
=?Utf-8?B?ZnV6enkzMzM=?=


 
 

function return values, console output

I'm trying to figure out how to run msbuild in a function and have this
function return true or false indicating msbuild's success or failure.
Also important, I want the normal output of the command (streaming and
with intended foregroung/background color) to appear in the console as
msbuild is running so, for example:

# ---------------------------------
function Compile() {
msbuild
if( $LastErrorCode -eq 0 ) {
return $true
} else {
return $false
}
}

#$compileSuccess = Compile
Compile

Calling "Compile" without assigning it to a variable I get the behavior that
I'm looking for, but no return value. However calling Compile and assigning
the return value to $compileSucsess as in the commented line in the example
causes all output from msbuild from to be stored in $compileSuccess variable,
not just the boolean return value, and msbuild's output doesn't appear in the
console as intended.

So how can I acheive the behavior I'm looking for? Is there a way to simply
return a value, not all output from the function?

My System SpecsSystem Spec
Old 08-21-2006   #2 (permalink)
=?Utf-8?B?L1wvXG9cL1wvIFtNVlBd?=


 
 

RE: function return values, console output

You are putting the output and the boolean to the pipeline
(return is optional all output gets returned.

you can use

MSbuild | out-host

this will output the test to the console but not the pipeline

MowPS>Function test {
>> ipconfig | out-host
>> if( $LastErrorCode -eq 0 ) {
>> return $true
>> } else {
>> return $false
>> }
>> }
>>

MowPS>test

Windows IP Configuration
Ethernet adapter Local Area Connection:
....

False

MowPS>$test = test

Windows IP Configuration
Ethernet adapter Local Area Connection:

MowPS>$test
False

b.t.w. this also has the lineadding bug theat is bothering a lot of the
powershell output.

Greetings /\/\o\/\/


"fuzzy333" wrote:

> I'm trying to figure out how to run msbuild in a function and have this
> function return true or false indicating msbuild's success or failure.
> Also important, I want the normal output of the command (streaming and
> with intended foregroung/background color) to appear in the console as
> msbuild is running so, for example:
>
> # ---------------------------------
> function Compile() {
> msbuild
> if( $LastErrorCode -eq 0 ) {
> return $true
> } else {
> return $false
> }
> }
>
> #$compileSuccess = Compile
> Compile
>
> Calling "Compile" without assigning it to a variable I get the behavior that
> I'm looking for, but no return value. However calling Compile and assigning
> the return value to $compileSucsess as in the commented line in the example
> causes all output from msbuild from to be stored in $compileSuccess variable,
> not just the boolean return value, and msbuild's output doesn't appear in the
> console as intended.
>
> So how can I acheive the behavior I'm looking for? Is there a way to simply
> return a value, not all output from the function?

My System SpecsSystem Spec
Old 08-21-2006   #3 (permalink)
Keith Hill [MVP]


 
 

Re: function return values, console output

"/\/\o\/\/ [MVP]" <oMVP@discussions.microsoft.com> wrote in message
news:3B33C30E-393F-4398-A9FF-0323EC388D41@microsoft.com...
> b.t.w. this also has the lineadding bug theat is bothering a lot of the
> powershell output.


You can say that again. :-)

--
Keith


My System SpecsSystem Spec
Old 08-22-2006   #4 (permalink)
=?Utf-8?B?ZnV6enkzMzM=?=


 
 

RE: function return values, console output

Thanks for replying but this doesn't address the issue.Using out-host in this
manner causes output to be buffered before it get to out-host, so it appears
in one big glob when msbuild is done, and all of the coloring that is done by
msbuild to it's output is also lost in this scenario.

Here's a little something I found though.
https://connect.microsoft.com/feedba...7986&SiteID=99
I think this addresses what I want, simply that msbuild writes to the
console directly, which would allow it to behave exactly as it did in cmd.exe.

"/\/\o\/\/ [MVP]" wrote:

> You are putting the output and the boolean to the pipeline
> (return is optional all output gets returned.
>
> you can use
>
> MSbuild | out-host
>
> this will output the test to the console but not the pipeline
>
> MowPS>Function test {
> >> ipconfig | out-host
> >> if( $LastErrorCode -eq 0 ) {
> >> return $true
> >> } else {
> >> return $false
> >> }
> >> }
> >>

> MowPS>test
>
> Windows IP Configuration
> Ethernet adapter Local Area Connection:
> ...
>
> False
>
> MowPS>$test = test
>
> Windows IP Configuration
> Ethernet adapter Local Area Connection:
>
> MowPS>$test
> False
>
> b.t.w. this also has the lineadding bug theat is bothering a lot of the
> powershell output.
>
> Greetings /\/\o\/\/
>
>
> "fuzzy333" wrote:
>
> > I'm trying to figure out how to run msbuild in a function and have this
> > function return true or false indicating msbuild's success or failure.
> > Also important, I want the normal output of the command (streaming and
> > with intended foregroung/background color) to appear in the console as
> > msbuild is running so, for example:
> >
> > # ---------------------------------
> > function Compile() {
> > msbuild
> > if( $LastErrorCode -eq 0 ) {
> > return $true
> > } else {
> > return $false
> > }
> > }
> >
> > #$compileSuccess = Compile
> > Compile
> >
> > Calling "Compile" without assigning it to a variable I get the behavior that
> > I'm looking for, but no return value. However calling Compile and assigning
> > the return value to $compileSucsess as in the commented line in the example
> > causes all output from msbuild from to be stored in $compileSuccess variable,
> > not just the boolean return value, and msbuild's output doesn't appear in the
> > console as intended.
> >
> > So how can I acheive the behavior I'm looking for? Is there a way to simply
> > return a value, not all output from the function?

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Return values from functions PowerShell
How to combine/return multiple values from a sql query VB Script
Associative Array - how to return mapped values? PowerShell
BUG: Redirecting function contents to a file truncates function lines at the width of the console PowerShell
function: Showing valid parameter values for a set 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