![](/img/trans.png)
[英]Passing Python variable to Powershell parameter using Python's Popen
[英]Passing Python variable to Powershell parameter using Popen
給你一個...
我正在使用 Python 的subprocess.Popen
從我的 Python 代碼中調用 Powershell 腳本; 有一個 Powershell 腳本需要一個初始輸入參數,然后在最后輸入“Enter”才能退出。 這個給我帶來了一些麻煩; Powershell 代碼有點像這樣:
$usrFileName = read-host "Please enter a file name to save the report"
*does some computering*
*creates some output* #ideally looking to capture this to output, but not the main problem
#display task complete to user
Read-Host -Prompt "Press Enter to exit" #This is problematic - don't know how to end this
蟒蛇代碼:
from tkinter import *
import os, datetime
import subprocess
from subprocess import Popen, PIPE
from keyboard import press_and_release
window = Tk()
window.title( 'PowerShell Script' )
frame = Frame(window)
fldrPath = r"C:/Users/paul.sisson/Downloads/Powershell Development/Monthly PMs PowerShell scans/BLDG-7UP-SUNY-Scans/7UP-LAB-A-325/"
listbox = Listbox(frame)
listbox.configure(width=50)
# Get todays date and add the lab name to be piped later
today = (datetime.datetime.now().strftime('%d%b%Y')).upper()
usrFileName = today + "-7UP-A-325"
for name in os.listdir(fldrPath):
listbox.insert('end', name)
def selection():
fileList = listbox.curselection()
for file in fileList:
keyword = 'Scans'
os.chdir(fldrPath)
proc = subprocess.Popen(["powershell.exe", '-File', fldrPath + '\\' + listbox.get(file)], bufsize=0,
universal_newlines=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
#proc.stdin.write(usrFileName) works to name file, but then deadlocks
while proc.poll() is None:
p = proc.stdout.readline()
output.insert('end', p)
output.update()
output = Text(window, width=75, height=6, wrap=WORD, background="white")
btn = Button(frame, text='Run Script', command=selection)
btn.pack(side=RIGHT, padx=5)
listbox.pack(side=LEFT)
frame.pack(padx=30, pady=30)
output.pack(fill=BOTH, expand=1, padx=5, pady=5)
window.mainloop()
我已經使用proc.stdin.write(usrFileName)
成功傳遞了輸入,但我擔心這不是正確的方法。 考慮到我想讀和寫的事實,我不想冒險使進程陷入僵局,我很確定這種情況正在發生。
根據幾個線程嘗試了其他一些示例:
proc = subprocess.Popen(["powershell.exe", '-File', fldrPath + '\\' + listbox.get(file),'-usrFileName:',
propername], bufsize=0, universal_newlines=1,
stdout=subprocess.PIPE, stdin=subprocess.PIPE)
與去subprocess.call
一次或兩次,但我需要使用proc.poll的輸出,這樣就不會工作。
你可以看到我帶來了keyboard
來補救回車,但是代碼會在到達那里之前掛起,所以不確定這是否是要走的路。
長話短說- 我正在尋找一些指導,以正確地將usrFileName
寫入 Powershell,在開始時,從 Powershell 讀取輸出,然后在最后執行“按 Enter”。
我應該明確關閉stdin
,以便子進程停止掛起嗎?
我覺得.communicate()
或線程是要走的路,但我需要一些優雅的人來給我指路!
感謝支持。
PS 更改 Powershell 不是一個選項,不是我的代碼,只是嘗試使用已經存在的東西。 謝謝!
通過 stdin 回答交互式Read-Host
提示要求每個響應都以換行符終止。
因此,用2 個換行符結束 stdin 輸入:一個用於回答文件名提示,另一個用於回答退出提示:
proc.stdin.write(usrFileName + '\n\n')
注意:PowerShell 的 stdout 輸出也將包括提示信息和提交的響應,因此您在解析輸出時需要考慮到這一點; 具體來說,stdout 輸出將包括以下內容:
Please enter a file name to save the report: 10Sep2020--7UP-A-325
(例如), Press Enter to exit:
,加上一個額外的空行,因為退出提示的Read-Host
語句的輸出沒有被捕獲變量,因此是隱式輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.