Calling a powershell script through python popen is blocked

Not open for further replies.


Hi, I am trying to execute a powershell script scross two domains with no trust. I am using an xml-rpc server that uses python popen to call the script on the exchange server.

When running the script localy, it runs smooth:

Here is the script:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 #add exchange snap-in $pass = ConvertTo-SecureString -string "Password1" -AsPlainText -Force #create secure Password [string] $user = $args[0] #read one argument from cmdline $domain = "" $email = $user+"@" $email+=$domain #create the mailbox New-Mailbox -Name $user -Password $pass -UserPrincipalName $email -DisplayName $user -FirstName $user

This is how I execute it locally:

powershell.exe C:\xml-rpc-server\create_mailbox.ps1 myname

while running it using a python script (which I intend to use in the server) it says:

File C:\xml-rpc-server\create_mailbox.ps1 cannot be loaded because the executio

n of scripts is disabled on this system. Please see "get-help about_signing" fo

r more details.

At line:1 char:37

+ C:\xml-rpc-server\create_mailbox.ps1 <<<< Fwfggo
+ CategoryInfo : NotSpecified: :)) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException

Here is the python script:

from subprocess import Popen, PIPE import string, random class PSCaller(): def call(self, args, server_name='localhost',cwd=None,shell=False): p = Popen(args, stdout=PIPE, stderr=PIPE, shell=shell, cwd=cwd) out,err = p.communicate() rc = p.returncode if rc is 0: print 'Process in {0} output: {1}\n-------------------------\n'.format(server_name, out) else: print 'ERROR: ', 'return code from {0} is: {1}'.format(server_name, rc) print 'Process output:\n{0}'.format(out) return rc,out,err def create_mail_box(self): #create a random user name print 'create mailbox' username = "".join(random.choice(string.letters + string.digits) for i in xrange(6)) #command args command = ['powershell.exe', r"C:\xml-rpc-server\create_mailbox.ps1", username] #create the maillbox through powershell return_code, output, err = return return_code, output, err if __name__ == "__main__": caller = PSCaller() return_code, output, err = caller.create_mail_box() print err


Found it....

When tunning powershell scripts in a seperate process you can use, for instance:

powershell.exe -ExecutionPolicy RemoteSigned -File c:\xml-rpc-server\test.ps1
This does the trick.
Not open for further replies.