0

i have a powershell script i'm trying to execute via python subprocess. i use check_output imported from subprocess for other cmd line processes, so i am using it for the powershell script. inside my powershell script i'm loading some 3rd party modules and then executing a cmdlet from those modules. here is my python code..

from subprocess import Popen, STDOUT, check_output

cmd = 'C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe -nologo -file c:\\test.ps1'
            cmd_output = check_output(cmd, stderr=STDOUT)
            output = cmd_output.decode("utf-8").split('\n')

When the python code runs it takes some time where i assume it's loading the modules via the code i have in the powershell ps1 script , BUT i get an error that the cmdlet i was referencing inside my powershell script is "not recognized as the name of a cmdlet", which happens when the modules don't get loaded properly in the powershell script, so i don't think my modules are loading which is weird.. i also noticed when the ps1 script loads via python it runs for about 10 secs then returns, if i run the ps1 script (see further below) manually from command line it usually takes 60 secs to load the modules inside the ps1 script.

i also tried another method for sub proccess instead of check_output, see below, with no differences

cmd = Popen(['powershell.exe', '-nologo', '-file', 'c:\\test.ps1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

BUT i can open a cmd window and execute it manually perfectly fine using

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -nologo -file C:\test\test.ps1

which should be the same as i'm already doing in python :( so i'm at a loss why it's not working in python??

also my execution policy is as follows

PS C:\> Get-ExecutionPolicy -list

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    Unrestricted
 LocalMachine    Unrestricted
1
  • i also just set Process in execution policy to Unrestricted and that made no difference either Commented Mar 29, 2018 at 19:49

1 Answer 1

2

Try using unrestricted ExecutionPolicy parameter :

# python 2.7 and 3.4
import subprocess
fname = "C:\\Demo\\test.ps1"
p = subprocess.Popen(["powershell", "-ExecutionPolicy", "unrestricted", "-nologo", "-file", "%s"%(fname)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode == 0:  
  print("buffer :")
  print(out)
else:
  print("error  :")
  print(err)

The content of 'C:\Demo\test.ps1'

write-host $env:USERPROFILE;
Get-ExecutionPolicy -list;
try{
    Get-Module -Name VMware.VimAutomation.Core -ListAvailable | Import-Module;
    write-host 'ok import VMware';
}catch{
    write-host 'error import VMware';
    write-host $_;
}

The output after script execution (note the Process policy)

buffer :
C:\Users\MyUserName
ok import VMware
        Scope ExecutionPolicy
        ----- ---------------
       ...       ...
       ...       ...
       Process    Unrestricted
       ...       ...
       ...       ...

On system32 Module path is here: C:\WINDOWS\system32\WindowsPowerShell\v1.0\

Try using:

p = subprocess.Popen(["powershell", "-ExecutionPolicy", "unrestricted", "-nologo", "-command", "&{ Get-Module –ListAvailable }"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

for showing all available modules

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry i'm not showing your comment but this script work on Windows Home edition, maybe your are on Enterprise version and then take a look in GPO or try to run your Python script from an administrator account.
no GPO, it's in a lab environment :) plus i'm domain admin with local admin inside this test lab AD, yes it's windows 2012 server.. i tried and it get the same results it fails to see "Connect-VIServer" as a cmdlet because it's not loading the PowerCLI modules, i can't fig out why LOL it works fine from cmd line. just so you know, the line that is not loading in ps1 is "Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue" , that just loads my vmware Powercli 6.5 modules, then the next line is a call to the Connect-VIServer cmdlet..
it's like it's skipping over the Import-Module line in the ps1 file :( when i run this sucker in python, but from any windows cmd window 32bit or 64bit cmd.exe manually, the ps1 script works as designed, it loads the modules and the cmdlet line succeeds.
no i don't think that is it, i just tried bypass for heck of it... and same results.. so the error i get in "out" is "connect-viserver the term 'connect-viserver' is not regocnized as the name of a cmdlet... blah blah :), because the line before the connect-viserver cmdlet, was the line to load the modules, if the line to load the modules would work :) then the connect-viserver cmdlet would work... i'm just baffled , because python is running powershell.exe and loading the ps1 script but WHY is not working and loading the module line in the ps1 script. it's a very simple script the ps1 i mean.
yes i understand .. based on my first response python code try : p = subprocess.Popen(["powershell", "-ExecutionPolicy", "unrestricted", "-nologo", "-command", "&{ Get-Module –ListAvailable }"]..... for showing availables modules
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.