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!