3

I have a python script that builds commands based off input gotten via rest. The commands work when printed out and copied and pasted into powershell. My question is how to make it actually execute the commands?

So far I have tried writing the commands to a file in the same directory and running like so:

import subprocess, sys
p = subprocess.Popen(["powershell.exe", "test.ps1"],stdout=sys.stdout)
p.communicate()

But I get an error:

Traceback (most recent call last): File "C:/Users/pschaefer/src/python/executePowerShell.py", line 2, in p = subprocess.Popen(["powershell.exe", "test.ps1"],stdout=sys.stdout) File "C:\Python27\lib\subprocess.py", line 703, in init errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr) File "C:\Python27\lib\subprocess.py", line 850, in _get_handles c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) UnsupportedOperation: fileno

Update

removing ,stdout=sys.stdout1 gets rid of the previous error and now I see

test.ps1 cannot be loaded because running \r\nscripts is disabled on this system.

I have tried switching the command to ".\\test.ps1 Set-ExecutionPolicy -ExecutionPolicy unrestricted" and still have the issue.

Also, would it be possible to build the commands as strings and execute them one by one as built instead of creating a .ps1 file? Big kudos to anyone that can figure that out!

Patrick Schaefer
  • 821
  • 10
  • 20
  • 1
    you don't need `stdout=sys.stdout` it's default. Have you tried with just `os.system()` ? or `subprocess.check_call()` ? – Jean-François Fabre Apr 12 '17 at 20:24
  • ah removing the `stdout=sys.stdout` stopped the error I see in python shell and I do briefly see what looks to be a powershell window popup with an error there but I can't catch what it is. Any ideas on how to stop it from closing that I could add to the ps1 file? Sorry definitely a noob here with powershell – Patrick Schaefer Apr 12 '17 at 20:28
  • don't run it from your IDE, run it from a standard console – Jean-François Fabre Apr 12 '17 at 20:31

2 Answers2

2

Is your machine you are running the script on enabled to run powershell scripts? You can set this via the Set-ExecutionPolicy cmdlet, or you can pass the -ExecutionPolicy Bypass parameter to run the script with lowered permissions.

xlm
  • 6,854
  • 14
  • 53
  • 55
Preston Martin
  • 2,789
  • 3
  • 26
  • 42
  • This may be on to something as I can run the resulting ps1 file directly in powershell without issues. I've run the Set-ExecutionPolicy command but I still see error "test.ps1 cannot be loaded because running \r\nscripts is disabled on this system." when trying to run the python script – Patrick Schaefer Apr 12 '17 at 20:44
  • Try passing the -Executionpolicy Bypass flag along with the your .ps1 file name – Preston Martin Apr 12 '17 at 20:50
  • Note that I needed to run the command in both x64 and x86 version of PowerShell to get it running – Patrick Schaefer Apr 13 '17 at 14:39
  • @PatrickSchaefer that shouldn't be necessary, but I'm glad you got it working! I'm guessing whichever shell you updated second is the one that your program is using. – Preston Martin Apr 13 '17 at 17:18
1

Running such scripts from an IDE isn't supported because IDEs redirect stdout to their own stream, which doesn't support fileno properly most of the time.

Running those commands in a real system shell works, though.

The workaround which works in both shell & IDE is to remove stdout=sys.stdout but you can't get the error messages or retrieve the output, or even better: redirect the output/error streams using Popen capabilities:

import subprocess, sys
p = subprocess.Popen(["powershell.exe", "test.ps1"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out,err = p.communicate()
print(out,err)

on my machine, since test.ps1 doesn't exist, I get a clear powershell error message.

EDIT: my answer doesn't answer to your edit of your question which shows the actual error message now. This Q&A may help you: PowerShell says "execution of scripts is disabled on this system."

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • So with your script (in python shell) I can see an error: "test.ps1 cannot be loaded because running \r\nscripts is disabled on this system." If I run the script directly in poweshell I see a quick popup that disappears. If I run just the ps1 file in powershell it works as expected, thoughts? – Patrick Schaefer Apr 12 '17 at 20:42
  • I have found this: http://stackoverflow.com/questions/4037939/powershell-says-execution-of-scripts-is-disabled-on-this-system – Jean-François Fabre Apr 12 '17 at 21:24