1

I'm trying to execute a powershell script as a subprocess in Python, and pipe the output back into python. When I run this script via command line it works without a hitch, however now that I've thrown it into PyCharm it gets to the line with p.communicate and hangs.

I've printed the PATH from within pycharm and the CMD to compare, and they are line for line the same, not seeing many other answers as to why this wouldn't work.

Code:

p = subprocess.Popen(r'powershell.exe powershell\DNfinder.ps1 group "{}"'.format(group),
                       stdout=subprocess.PIPE)

print('Opened first subprocess') #This statement prints every time

groupDN = p.communicate()
Dejarr
  • 63
  • 5
  • If you use the full path to Powershell (`C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`), are you getting the same behavior? Environment variables are expectedly shared between Python and native Windows tools, but Python might not be able to look for exe's in Path as you're expecting. – scrthq May 10 '17 at 20:26
  • @nferrell Yes, it hangs in the same spot. Also if I change out the name of the script to an invalid one, it throws up a powershell error about it being invalid, so it seems to be getting as far as opening the script within powershell... – Dejarr May 10 '17 at 20:35
  • What parameters does your PS1 have? Trying to figure out what `group` and `"{}"` fill in. Assuming whatever parameters you have on the file have the Position attribute set, seeing as you're not naming any parameters after your call to the file? – scrthq May 10 '17 at 23:20
  • Also, is your file hitting any mapped drives that expand to a UNC path? If so, try inputting the full UNC path instead of the mapped drive letter – scrthq May 10 '17 at 23:21
  • @nferrell The PS1 is finding the Distinguished Name of an AD group. The script works for groups OR users, so the 'group' is the type of object it's looking for and the {} format is filling in the name of the group. With this use in python I only care about groups so I just added it onto the end instead of allowing it be changed. As for the UNC path question, everything should be on the local machine, it's just reaching out to AD for the info. – Dejarr May 11 '17 at 00:12
  • Gotcha - going through ADSI or using the ActiveDirectory module? I'm assuming `group` in your command is a variable set somewhere earlier in the Python script? – scrthq May 11 '17 at 00:35
  • @nferrell Yes, group is set earlier in the script. This ps1 is using ADSI, however I just changed it out for a script that had nothing but Write-Host 'Test', and it still hung on communicate. – Dejarr May 11 '17 at 14:07
  • http://stackoverflow.com/questions/21944895/running-powershell-script-within-python-script-how-to-make-python-print-the-pow --- I would check the answer there... not sure what your script is returning (full object, just the distinguishedName, etc), but I suggest updating your script to only return what you're expecting to store in your `groupDN` variable... also, I'm not exactly sure how some of the surrounding items or parameters in script are expected to work, so I suggest applying that answer to your Write-Host script to confirm that you have Powershell working 100% from Python – scrthq May 12 '17 at 01:42
  • @nferrell That's actually the SO post that I originally used to get this set up. Having said that, I figured out what the issue was. As soon as I set stdin and stderr to a PIPE as well, it worked. I have any idea as to why it needs to be this way in pycharm vs running commandline but it works! Thanks a bunch for your time and help! – Dejarr May 12 '17 at 14:01

1 Answers1

3

Ran python script successfully within PyCharm after adding

stdin=subprocess.PIPE 

and

stderr=subprocess.PIPE
Dejarr
  • 63
  • 5