python - 在python脚本中运行powershell脚本,如何让python在运行时打印powershell输出

标签 python windows python-2.7 powershell stdout

我正在编写一个 python 脚本,它检查各种条件并相应地运行一个 powershell 脚本,以帮助我自动从 Windows XP 迁移到 Windows 7。powershell 脚本提供自己的输出,让用户了解正在发生的事情。我想获取 powershell 脚本的输出并将其打印为 python 脚本的输出。我环顾了一些似乎想做同样事情但似乎对我不起作用的问题。最初我尝试使用

import subprocess
subprocess.call(["C:\Users\gu2124\Desktop\helloworld.ps1"])

正如这里的建议 Run PowerShell function from Python script但我发现这会等待程序先执行并且不会给出输出,所以我发现我需要使用 subprocess.Popen() ,正如此处建议的那样 Use Popen to execute a Powershell script in Python, how can I get the Powershell script's output and update it to web page?所以我试了一下

import subprocess
subprocess.Popen(["C:\Users\gu2124\Desktop\helloworld.ps1"], stdout=sys.stdout)

我得到这个错误

Traceback (most recent call last):
  File "C:\Users\gu2124\Desktop\pstest.py", line 5, in <module>
    subprocess.Popen(["C:\Users\gu2124\Desktop\helloworld.py1"], stdout=sys.stdout)
  File "C:\Python27\lib\subprocess.py", line 701, in __init__
    errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
  File "C:\Python27\lib\subprocess.py", line 848, in _get_handles
    c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
  File "<string>", line 523, in __getattr__
  File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\netref.py", line 150, in __getattr__
    return syncreq(self, consts.HANDLE_GETATTR, name)
  File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\netref.py", line 71, in syncreq
    return conn.sync_request(handler, oid, *args)
  File "C:\Program Files\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 434, in sync_request
    raise obj

AttributeError: DebugOutput instance has no attribute 'fileno'

我不完全确定这意味着什么,但在阅读此 AttributeError: StringIO instance has no attribute 'fileno' 后我认为我理解了是因为我错误地弄乱了标准输出。我环顾四周,发现了这个 Why won't my python subprocess code work?答案说要使用 stdout=subprocess.PIPE 所以我试了一下

import subprocess
subprocess.Popen(["C:\Users\gu2124\Desktop\helloworld.ps1"], stdout=subprocess.PIPE)

这也不给我输出 终于看到这个http://www.pythonforbeginners.com/os/subprocess-for-system-administrators并将我的代码更改为此

import subprocess
p = subprocess.Popen(["powershell","C:\Users\gu2124\Desktop\helloworld.ps1"], stdout=subprocess.PIPE)
print p.communicate

我认为这可能是因为我最初尝试从命令行运行 powershell 脚本,所以我必须先打开 powershell。当我直接在命令行中键入这些命令时,它会按应有的方式工作,但是当我通过 python 脚本运行它时,它会给出这个

<bound method Popen.communicate of <subprocess.Popen object at 0x00000000026E4A90>>

我猜这是一个改进,但不是我期待的“Hello world”。 我不知道接下来我应该尝试做什么才能让它发挥作用。任何帮助将不胜感激

此外,如果这里需要我正在使用的 powershell 脚本,它是

$strString = "Hello World"
write-host $strString

function ftest{
$test = "Test"
write-host $test
}

编辑:我尝试像第一个答案中建议的那样升级到 python 3.3,但我仍然无法让它工作。我使用了命令 p = subprocess.Popen(['powershell.exe', "C:\\Users\\gu2124\\Desktop\\helloworld.ps1"], stdout=sys.stdout)并确定文件在那里,但出现此错误:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    p = subprocess.Popen(['powershell.exe', "C:\\Users\\gu2124\\Desktop\\helloworld.ps1"], stdout=sys.stdout)
  File "C:\Python27\lib\subprocess.py", line 701, in __init__
    errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
  File "C:\Python27\lib\subprocess.py", line 848, in _get_handles
    c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
UnsupportedOperation: fileno 

最佳答案

  1. 确保您可以运行 powershell 脚本(默认情况下禁用)。很可能你已经这样做了。 http://technet.microsoft.com/en-us/library/ee176949.aspx

    Set-ExecutionPolicy RemoteSigned
    
  2. 在您的 powershell 脚本 helloworld.py 上运行此 python 脚本:

    # -*- coding: iso-8859-1 -*-
    import subprocess, sys
    
    p = subprocess.Popen(["powershell.exe", 
                  "C:\\Users\\USER\\Desktop\\helloworld.ps1"], 
                  stdout=sys.stdout)
    p.communicate()
    

此代码基于 python3.4(或任何 3.x 系列解释器),但它也适用于 python2.x 系列。

C:\Users\MacEwin\Desktop>python helloworld.py
Hello World

关于python - 在python脚本中运行powershell脚本,如何让python在运行时打印powershell输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21944895/

相关文章:

python - 尝试部署 Flask 应用程序时从 beanstalk 获取错误 : "no module named flask"

windows - 奇怪的 Qt SSL 问题——出现错误 "No Error",没有别的,如果我忽略它,一切正常

Python:有没有办法安排 Celery 任务只在特定时间范围内运行?

python - 使用 Selenium 每天下载不同的文件

python - 提取文本文件中每次出现的两个子字符串之间的文本

python - openpyxl 中的水平文本对齐

python socketserver连接超时

php - PHP 7.1+ Windows readline扩展并非所有功能都存在

python - 如何在 Windows 8 中为 python 设置 pip

c++ - 在 Windows 桌面共享 API 上,UAC 提示变为黑色暂停屏幕