1

Forgive me, I am new to PowerShell in general. I'm updating a build process that works on Linux (in bash) to one that will work on Windows in PowerShell.

My goal is to get the version of the game engine currently present on the build system. The default build location is well-known, so we try to execute it and get the version, like so:

$Version = & 'C:\Program Files\LOVE\love.exe' --version

When this executes, the $Version value is empty:

Write-Output $Version
[no output]

$Version -Eq $True 
False

If I run my executable directly from the shell, I notice the line is not presented on a newline:

PS C:\Users\robbm\Myproject\Mygame> $Version = & 'C:\Program Files\LOVE\love.exe' --version
PS C:\Users\robbm\Myproject\Mygame> LOVE 11.3 (Mysterious Mysteries)

This makes me suspect there is some strange output behavior with the executable in the first place. Is this a problem with LÖVE's --version output, or am I misunderstanding something about redirecting outputs in PowerShell? I've tried a few things to capture output, and $Version always seems to end up a nil value, such as:

$Version = & '\\build\love\love.exe' '--version' | Out-String
Write-Output $Version
$Version = (& '\\build\love\love.exe' '--version' | Out-String)
Write-Output $Version

Help is appreciated. As this works for other cmdlets, I'm inclined to believe it might be a function of LÖVE, but I'd appreciate thoughts as to how I could work with this anyway, or any method in which to capture the version it's clearly outputting to the screen when I execute it directly regardless.

EDIT:

LÖVE definitely does something different in regards to running on Windows. Looking at the version printing source, we are working with the aptly-named LOVE_LEGENDARY_CONSOLE_IO_HACK enabled on Windows, which appears to open a new console entirely, perhaps in cmd and write out there.

Doing the suggestions of commenters, I tried doing the .Exception.Message method, but there is none when called like so:

$Version = &('C:\Program Files\LOVE\love.exe' '--version').Exception.Message

So I'm still looking for ways to make this work within the confines of LÖVE hacking together some strange I/O stream.

EDIT2:

Another fun fact, redirection to a file similarly fails:

PS> (&'C:\Program Files\LOVE\love.exe' '--version') 2>&1 > .\love.txt

PS> LOVE 11.3 (Mysterious Mysteries)

PS> cat .\love.txt
[empty]

So this looks to be overly hacky on behalf of LÖVE, not an issue with PowerShell.

robbmanes
  • 319
  • 3
  • 10
  • 1
    I don't have the executable you describe, but this works fine for me with NodeJs - `$nodejs = & 'C:\Program Files\nodejs\node.exe' --version`. If you run `love.exe /?` or `love.exe /h` does it confirm the command line options in Windows? – Ash May 09 '20 at 15:01
  • 3
    It might be that [the executable prints to stderr](https://stackoverflow.com/questions/36953800/powershell-script-to-check-if-python-is-installed/36954394#36954394) – Mathias R. Jessen May 09 '20 at 15:01
  • 2
    There is also that some exes print to host instead of output, whatever the command line terms are for that. For example older versions of 7zip could have the output captured and parsed, which was great for capturing and calculating progress, which I used for a few things, but later versions write directly to the console to display a live percentage that updates that line instead of printing on new lines, which cannot be put into a variable or run through foreach or anything. – Deadly-Bagel May 09 '20 at 15:33

1 Answers1

2

After reading your last edit, this probably won't help you, but may help others.

You could try to capture the output like this:

function runCmdAndCaptureOutput(
    [Parameter(Mandatory=$true)]
    [string] $cmd
) {
    [string] $errOut
    [string] $stdOut
    # Deliberately dropped '$' from vars below. 
    Invoke-Expression $cmd -ErrorVariable errOut -OutVariable stdOut   
    if($LASTEXITCODE -ne 0) {
        Write-Host -ForegroundColor Red "LASTEXITCODE: $LASTEXITCODE"
        throw $LASTEXITCODE
    } 
    return $stdOut
}

$exeCmd = "'C:\Program Files\LOVE\love.exe' --version"
$output = runCmdAndCaptureOutput -cmd $exeCmd
Write-Host $output
derekbaker783
  • 8,109
  • 4
  • 36
  • 50