0

I need to obtain the version of Maven's binary but using an explicit call to the program bring up a secondary windows and doesn't work.

In this case, it spawns a new window and returns nothing:

# spawns a new window with an error
# wanted to be more explicit in my call to the program
Write-Host "Maven..."
$x = & 'C:\path\to\maven\bin\mvn' '-version' 2>&1
Write-Host "out: "$x

In this case, it works as intended, but I have to assume the environment variable is configured:

# works as intended
# implies path is set
Write-Host "Maven..."
$x = & 'mvn' '-version' 2>&1
Write-Host "out: "$x

Any reasons why this is happening? Ideally, I'd like the first method to work.

Edit #1

Been trying the following, using Start-Process (from Capturing standard out and error with Start-Process)

Try {
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo
    $pinfo.FileName = "C:\path\to\maven\bin\mvn"
    $pinfo.RedirectStandardError = $true
    $pinfo.RedirectStandardOutput = $true
    $pinfo.UseShellExecute = $false
    $pinfo.Arguments = "-version"
    $p = New-Object System.Diagnostics.Process
    $p.StartInfo = $pinfo
    $p.Start() | Out-Null
    $stdout = $p.StandardOutput.ReadToEnd()
    #$stderr = $p.StandardError.ReadToEnd()
    #$exitCode = $p.ExitCode  
    $p.WaitForExit()

    Write-host "out: "$stdout
}
Catch {
    Write-Host  $_.Exception.Message
}

I get this error:

Exception calling "Start" with "0" argument(s): "The specified executable is not a valid application for this OS platfor m."

TechFanDan
  • 3,329
  • 6
  • 46
  • 89
  • You could use [Start-Process](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1) to have a better control about the started process. – Olaf Jun 20 '18 at 14:44
  • @Olaf how about storing the output to a variable? I was under the impression that I had to output to a file using this method. – TechFanDan Jun 20 '18 at 14:51
  • That's right. A temporary file could be an option. I remember I have read in one question here in SO about something similar (redirecting standard or error output to a variable). You should search for. – Olaf Jun 20 '18 at 14:58
  • Just a weird idea: did you try to add the extension in your first method? – Olaf Jun 20 '18 at 15:01
  • @Olaf there is no extension to provide. – TechFanDan Jun 20 '18 at 15:17
  • 1
    The error says it all. The binary `mvn` is not a valid binary/executable for the OS you are executing it on. You need a version which could be executed on your OS. – tukan Jun 20 '18 at 15:41
  • `'C:\path\to\maven\bin\mvn'` might not be the correct and complete path to your executable. That's why it's failing when you try to run it with this path. You should check the path variable in your environment. The path might point to the right directory. – Olaf Jun 20 '18 at 17:37
  • Are you running this is a "PowerShell x86" console but the Maven executable is x64? Or are you trying to run the x64 Maven exe from a 32 bit version of Windows? – BenH Jun 20 '18 at 21:52
  • @BenH If I simply run "mvn -version", from the same window, it works. If I specify the full path, it doesn't. – TechFanDan Jun 21 '18 at 12:08
  • @Olaf, I copied the path to the Maven binary using Windows Explorer... – TechFanDan Jun 21 '18 at 12:09

0 Answers0