我正在写一个python脚本,它检查各种条件,并相应地运行powershell脚本,以帮助我从Windows XP自动迁移到Windows 7. PowerShell脚本提供了自己的输出,让用户更新正在发生的事情。 我想获取powershell脚本的输出,并将其打印为python脚本的输出。 我环顾了一些似乎想要做同样事情的问题,但他们似乎并没有为我工作。 最初我尝试使用
import subprocess subprocess.call(["C:\Users\gu2124\Desktop\helloworld.ps1"])
正如这里所build议的从Python脚本运行PowerShell函数,但我发现这等待程序先执行,并没有给出输出,所以我发现我需要使用subprocess.Popen subprocess.Popen()
因为在这里suggusted 使用 Popen 执行PowerShell脚本在Python中,我怎样才能得到Powershell脚本的输出,并将其更新到网页? 所以我试了这个
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实例没有属性“fileno”是因为我搞错了stdout不正确。 我看了更多,我发现这为什么不会我的Pythonsubprocess代码工作? 答案说使用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 }
编辑:我试图升级到python3.3像第一个答案中的build议,但我仍然无法得到它的工作。 我使用了命令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
确保你可以运行PowerShell脚本(默认是禁用的)。 可能你已经做到了。 http://technet.microsoft.com/en-us/library/ee176949.aspx
Set-ExecutionPolicy RemoteSigned
在你的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 2.7,但在Python 3.3中调用Popen
与stdout
设置为sys.stdout
工作得很好。 不过,我还没有逃过路上的反斜杠。
>>> import subprocess >>> import sys >>> p = subprocess.Popen(['powershell.exe', 'C:\\Temp\\test.ps1'], stdout=sys.stdout) >>> Hello World _