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."