Home > Enterprise >  How to keep subprocess powershell session going throughout python
How to keep subprocess powershell session going throughout python

Time:04-25

I am building an email purge tool. The premise is that the .py needs to connect to the IPPSSession using Powershell. like so:

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

However, when I go running the commands later in the program, it does not recognize the commands. From what I have read, it appears (subprocess) sp.run is connecting and promptly disconnecting. For the commands later in the program to be recognized, I need to maintain a connection.

Is there a way to have the IPPSSession run the entire length of the program? I guess I could rewrite the whole program in PowerShell exclusively....

CodePudding user response:

After some stimulants and quite a bit of thinking. I found a better way to format the query. Behold:

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]")

The session runs the whole length of the Var. so all of the input happens first, and then it executes and breaks the session cleanly.

  • Related