如何保持子进程 powershell 会话贯穿始终 python

How to keep subprocess powershell session going throughout python

我正在构建一个电子邮件清除工具。前提是.py需要使用Powershell连接IPPSSession。像这样:

sp.run(f"Connect-IPPSSession -UserPrincipalName {Email}", shell=True)

但是,当我稍后在程序中执行命令时 运行,它无法识别这些命令。 据我所读,似乎 (subprocess) sp.run 正在连接并立即断开连接。 为了程序后面的命令能够被识别,我需要保持连接。

有没有办法让 IPPSSession 运行 整个程序长度?我想我可以专门用 PowerShell 重写整个程序....

经过一些兴奋剂和相当多的思考。我找到了一种更好的格式化查询的方法。看:

Email_Purge = f"Connect-IPPSSession -UserPrincipalName {Email} ; New-ComplianceSearchAction -SearchName {Search_Name!r} -purge -PurgeType {Purge_Type}"

if Purge_Type == "SoftDelete" or Purge_Type == "HardDelete":
    sp.run(Email_Purge, shell=True)
else:
    print("Please enter [SoftDelete] or [HardDelete]")

会话运行整个 Var。所以所有的输入首先发生,然后它执行并干净地中断会话。