1

In my powershell script I am trying to get the python version in windows server 2008. This script just prints the version in console but I am not getting the value into variable.

Code:

$python = & python -V
write-host "Python Version: " + $python

Expected output:

Python Version: Python 2.7.8

Actual output:

Python 2.7.8
Python Version:

Any one help me out.

Mohan
  • 143
  • 10
  • 1
    I was unable to reproduce this with something simple like `& ping -n 1 localhost`. So I actually intalled Python to test just in case and your code does work. I had to dot source python.exe for testing but `$python = &.\python.exe -V` but the output worked fine. I'm also using PowerShell 3.0. What happens when you try `$python = & ping -n 1 localhost` and `write-host "Python Version: $python"`.FYI variable will expand inside doublequotes. – Matt Sep 19 '14 at 13:30
  • Thanks @Matt. When I use $python = & ping -n 1 localhost code I can get the output of Python Version: Pinging AWSRIA185.one.ads.bms.com [::1] with 32 bytes of data: Reply from ::1: time<1ms Ping statistics for ::1: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), App roximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms. – Mohan Sep 19 '14 at 14:33
  • If I run the code $python = &.\python.exe -V then I get the output of & : The term '.\python.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. – Mohan Sep 19 '14 at 14:35
  • In my example i was dot sourcing the python executable since it was not part of my path. That was not meant to be a fix or anything. It just means that in your current directory in PowerShell you dont have a `python.exe`. The ping command in `Write-Host` does work. Interesting. Also use a backtick for code in comments. – Matt Sep 19 '14 at 14:39
  • for code `$puthon = & .\python.exe -V write-host "Python Version: $python"` my output is `Python 2.7.8 Python Version: ` – Mohan Sep 19 '14 at 14:48

2 Answers2

3

python.exe -V writes the version information to the Error output stream instead of the Success output stream. You get the observed output because the "error" message is printed before Write-Host is run, and the first command produces no output on the Success output stream to be assigned to $python, so the variable is empty when used in the Write-Host statement.

Change this line:

$python = & python -V

to this:

$python = & python -V 2>&1

and the problem will disappear.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Your code should work as designed. For whatever reason it is sending output to console instead of the variable. This code does work for me on PowerShell 3.0

$python = & python -V
write-host "Python Version: " + $python

You even mention in comments that similar commands work as expected like the following.

$python = & ping -n 1 localhost
write-host "Python Version: $python"

While I am not sure of the root cause you can write some code that will force capture of standard output into a System.Diagnostics.Process object. I took the code from another SO answer.

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "C:\Python34\python.exe"
$pinfo.Arguments = "-V"
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
Write-Host "Python Version: $stdout"

I am not sure why a simple two liner does not work for you. What happens when you do something like this?. Again I dont have Python in my Path variable so i used the fullname of the file in my example.

Write-Host "Python Version: $(&"C:\Python34\python.exe" -V)"
or
Write-Host "Python Version: $(& python.exe -V)"
Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
  • `$python = & python -V 2>&1` this line solves my problem. Thanks @Matt for the response. – Mohan Sep 19 '14 at 15:42