简体   繁体   中英

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 = datetime.datetime.now().strftime("%Y-%d-%m-%H-%M")

cmd = '. %s; %s -args xyz %s %s %s' % \
      (script_path,
       function_name,
       file_path,
       subscription_id,
       now)
process = subprocess.Popen(
    [path, '-ExecutionPolicy', 'Unrestricted',
     cmd
     ], 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")' % \
          (script_path,
           function_name,
           file_path,
           subscription_id,
           now)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM