簡體   English   中英

Python,Powershell和Popen

[英]Python, Powershell, and Popen

我正在嘗試編寫一個Python程序,它將打開一個PowerShell進程,然后在將命令傳遞給它並獲取輸出時保持打開狀態。 我不想為每個命令打開一個新的PowerShell進程,因為powershell會話必須通過遠程系統進行身份驗證,而且我不想經常使用每個命令進行非超快速的身份驗證過程。

然而,正在發生的事情是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,但它完全改變了我與子進程交互的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM