python - 从Windows Server 2008中的powershell 3.0脚本获取python版本

标签 python powershell

在我的 powershell 脚本中,我试图获取 Windows Server 2008 中的 python 版本。该脚本仅在控制台中打印版本,但我没有将值放入变量中。

代码:

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

预期输出:

Python Version: Python 2.7.8

实际输出:

Python 2.7.8
Python Version:

谁能帮帮我。

最佳答案

python.exe -V将版本信息写入 Error 输出流而不是 Success 输出流。您会得到观察到的输出,因为“错误”消息是在 Write-Host 之前打印的。运行后,第一个命令在要分配给 $python 的成功输出流上不产生任何输出。 ,因此在 Write-Host 中使用时该变量为空声明。

更改此行:

$python = & python -V

对此:

$python = & python -V 2>&1

问题就会消失。

关于python - 从Windows Server 2008中的powershell 3.0脚本获取python版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25933414/

相关文章:

java - 如何在 java 中运行以下 Powershell 命令?

networking - Powershell 在当前服务器上创建共享 - 授予用户/组权限

powershell - 将Powershell中的默认值传递给另一个脚本

Windows 脚本 : Registry Import from Pipe/STDIN?

python - 将数据框列文本包装为具有不同长度约束的多个列

python - 为什么 pcap_dispatch 只返回 1 个数据包?

c++ - SWIG C++ 到 Python:将 std::list 作为参数返回给 Python

python - 谷歌语音识别模块在我尝试时抛出错误

Powershell 脚本默认值未显示在 Get-Help -full 中

python - pandas read_csv skiprows - 确定要跳过的行