2017-07-22 22 views
0

運行時,我寫了一個PowerShell腳本,當我運行CMD,從作爲不能在PowerShell中導入AzureRM模塊在Python

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

Azure.ps1使用一些功能從AzureRM PowerShell的模塊,它工作正常。

但是,當我運行相同的命令從Python作爲,

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() 

在這種情況下,PowerShell是不加載AzureRM模塊;這導致整個腳本失敗。

我們該如何強制加載AzureRM模塊?

試過但沒有工作。

一)在文件Azure.ps1
b)中開始時加入Import-Module AzureRM改變CMD作爲

cmd = 'Import-Module AzureRM; . %s; %s(%s, %s, "%s")' % \ 
      (script_path, 
      function_name, 
      file_path, 
      subscription_id, 
      now) 
+0

您運行的是哪個版本的Python?在給出打印語句的情況下,我從2.x中猜出了一些東西,但這會隨着'subprocess'隨着時間的推移而改變。 –

+0

我的Python版本是3.6.1,p = subprocess.Popen([「powershell.exe」,「-ExecutionPolicy」,「Unrestricted」,「C:\\ Users \\ jason \\ Desktop \\ 11 \\ jasonlogin。 ps1「],stdout = sys.stdout)適用於我,我可以使用ARM命令獲取信息,請問您是否顯示您的ps1和錯誤日誌? –

+0

@DavidMetcalfe Python2.7是的它的工作原理,我需要使用python64位。 –

回答

0

Powershell的模塊被安裝在系統64。

我不得不安裝Pythonx64並使用它與PowerShell進行通信。

我認爲原因是,當我們使用Pythonx86時,它通過x86進程與Powershell進行通信,在Pyhonx64的情況下,它使用x64進程與PowerShell進行通信。