gpt4 book ai didi

python - Python,Powershell和Popen

转载 作者:行者123 更新时间:2023-11-28 19:19:50 25 4
gpt4 key购买 nike

我正在尝试编写一个Python程序,该程序将打开Powershell进程,然后在将命令传递给它并获取输出时保持打开状态。我不想为每个命令打开一个新的Powershell进程,因为Powershell session 必须通过远程系统进行身份验证,并且我不想对每个命令都不断地进行非超快速的身份验证过程。

但是,发生的事情是powershell进程正在接管并阻止Python代码的继续执行... Powershell进程启动后,Python进程将立即停止并等待其退出。显然,这不好,因为一旦Powershell启动,我就无法将任何命令传递给它。

我的代码的相关部分是:

import subprocess as sp

def _open_powershell_session():

ps_path = r"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"
azure_path = r"C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\ShortcutStartup.ps1"
flags = ["-NoExit", "-ExecutionPolicy", "Bypass", "-File"]
launch_str = [ps_path] + flags + [azure_path]

ps_session = sp.Popen(args=launch_str, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)

return ps_session

我究竟做错了什么?

最佳答案

看看Twisted:https://twistedmatrix.com/documents/14.0.0/core/howto/process.html

我没有在PowerShell.exe中使用它,但是它完全改变了我与子进程进行交互的方式。

关于python - Python,Powershell和Popen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com