0

Writing a powershell script with a function that invokes an executable, and then attempts to return a bool. Two errors occur:

  1. The output from the .exe doesn't show in the console window while running
  2. the returned value is contains the output + the return value..
    I'm doing something stupid, but what?

Script is

function HelloWorld () {
    $cmd = "./HelloWorld.exe"
    & $cmd
    return $true}
$ret = HelloWorld

The $ret = "Hello World
True"

1
  • @Egon Stetmann. Why!? The semicolon in PowerShell is only valid in some conditions. I.e., all doe on the same line, etc. What the OP is doing is not valid for what he's expecting. PowerShell only runs PowerShell code. You can call exe's from PowerShell, (which under the covers is calling cmd.exe to run the exe) but the exe is in control. Commented Jun 4, 2021 at 0:01

3 Answers 3

2

UPDATED: Ok, I found a script online to create the helloworld.exe

Add-Type -outputtype consoleapplication -outputassembly helloworld.exe 'public class helloworld{public static void Main(){System.Console.WriteLine("hello world");}}'

Now We can run this:

function HelloWorld {

    $cmd = ".\HelloWorld.exe"
    & $cmd | Write-Host

    return $true
 }

$ret = HelloWorld

After run:

hello world

PS> $ret
True

PS> $ret | gm


   TypeName: System.Boolean

Seems to work here at least.

HTH

Sign up to request clarification or add additional context in comments.

Comments

2

The output is being captured by the assignment to $ret. Important to understand PowerShell functions return all output. when you ran & $cmd the output is returned, then you ran an explicit albeit unnecessary Return $true. Both pieces of data get returned and you see no screen output because it was consumed by the assignment.

In order to get the output of HelloWorld.exe to the console while only returning the Boolean from the function you can use either Write-Host or Out-Host. The difference being the latter traverses PowerShell's for-display formatting system, which may not be necessary in this case.

function HelloWorld () 
{
    $cmd = "./HelloWorld.exe"
    & $cmd | Write-Host
    return $true
}

$return = HelloWorld

In this case the screen should show the output from HellowWorld.exe, but $return should only contain $true.

Note: Because of the aforementioned behavior Return isn't technically necessary. The only necessary use case for Return is to explicitly exit a function usually early.

Also note: this assumes HelloWorld.exe is a typical console application for which PowerShell will capture output into the success stream.

2 Comments

Yes, this is the answer.
Well said, definitely agree with Santiago!
0

So in the function, there is a call to an executable which had a console.writeline statement (console.writelne("Hello world"))

calling the function works fine, but there no output that appears. Instead it got returned to the caller along with the return true/false. How can I get just the bool return without disrupting the output from the exe which we need to see on the console real time...

3 Comments

Your .exe is writing to its own Window and then terminating so you don't see the output. Try putting a read in your .exe after the write and see if this is so . Your .exe would have to use Windows calls to find the powershell window then redirect your output there. Unless I am missing something here?
If I do not try to pick up the return code (ie $ret=HelloWorld) and just call HelloWorld by itself, the console.writeline shows up. but then I can't capture a success return code... Grr...
So you want capture the bool and display the output of your .exe on the PS Host? If that's so you need to direct the output of your .exe to the Information Stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.