3

I'm trying to write a Python program that will open a powershell process, then keep it open while passing commands to it and getting the output. I don't want to open a new powershell process for each command because the powershell session must authenticate with a remote system, and I don't want to constantly go through the not-super-fast authentication process with every command.

What's happening though is that the powershell process is taking over and blocking continued execution of the Python code... as soon as powershell process starts, the Python process halts and waits for it to exit. Obviously this is no good because as soon as powershell launches I can't pass any commands to it.

The relevant portion of my code is:

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

What am I doing wrong?

learningKnight
  • 321
  • 1
  • 3
  • 14
  • You're not wanting the PowerShell.exe process, you want the PowerShell object. I don't think you can use the executable like this unless you are pushing keystrokes and scraping the screen output. – Mike Shepard Jan 21 '15 at 20:52
  • So you're telling me that the concept of opening a process and having Python pass data to stdin, and then keeping that process open for future stdin calls from the Python program, is impossible? It seems to me this would be a very basic use-case... opening a process and then communicating back and forth with it. – learningKnight Jan 21 '15 at 21:25
  • I don't know that it's impossible in general, just that I don't think you can use the PowerShell.exe executable that way. – Mike Shepard Jan 21 '15 at 21:28
  • Ah I see.. but why would that be? It's a console application so it should accept input from stdin. Is powershell unique in some way? – learningKnight Jan 21 '15 at 21:33
  • try adding `shell=True` to the arguments of `Popen` – Vasili Syrakis Feb 09 '15 at 15:15
  • `Popen()` does *not* wait the powershell to finish therefore the issue is in the code that is not shown in your question. Create a [minimal but complete code example](http://stackoverflow.com/help/mcve) -- start powershell, pass several commands, read their output. You should know the answer to the following questions: 1. does powershell write to stdout or console directly? 2. Can you force line-buffering (so that powershell would flush its stdout on newline)? 3. How do you know where an individual command's output ends e.g., where is eof for file1 if you pass two commands: `cat file1`, cat f2 – jfs Feb 09 '15 at 16:26
  • here's [code example that shows how `subprocess.Popen` could be used interactively](http://stackoverflow.com/a/23795689/4279) -- there are many gotchas (I've mentioned three in the previous comment). – jfs Feb 09 '15 at 16:31
  • You could try [pexpect's analog](https://pexpect.readthedocs.org/en/latest/FAQ.html#whynotpipe) for Windows such `winpexpect` (it is not maintained but it might work in your case), see [ftp example](http://stackoverflow.com/a/12606327/4279) – jfs Feb 09 '15 at 16:47

0 Answers0