sonu kumar sonu kumar - 3 years ago 108
Python Question

Can't import AzureRM module in PowerShell while running from Python

I wrote a PowerShell script which works fine when I run that from cmd as

powershell . C:\\scripts\\Azure.ps1; Review-Subscriptions -args xyz .\\output xxx-xxx-xxx 2017-23-07-01-04

Azure.ps1 uses some functions from AzureRM PowerShell module.

But when I run the same command from Python as,

path = "C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
script_path = "C:\\scripts\\Azure.ps1"
function_name = "Review-Subscriptions"
subscription_id = 'xxx-xxx-xxx'
file_path = '.\\output"'
now ="%Y-%d-%m-%H-%M")

cmd = '. %s; %s -args xyz %s %s %s' % \
process = subprocess.Popen(
[path, '-ExecutionPolicy', 'Unrestricted',
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print process.communicate()

In this case, PowerShell is not loading AzureRM module; which is causing entire script to fail.

How can we force to load the AzureRM module?

Tried following but didn't work.

a) added
Import-Module AzureRM
in the beginning of the file Azure.ps1

b) changed cmd as

cmd = 'Import-Module AzureRM; . %s; %s( %s, %s, "%s")' % \

Answer Source

Powershell modules were installed in the x64 system.

I had to just install Pythonx64 and use that to communicate with PowerShell.

I think the reason is that, when we use Pythonx86 then it communicates with Powershell by means of an x86 process, where as in the case Pyhonx64, it communicates with PowerShell using an x64 process.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download