2013-03-18 68 views
1

我有一個很長的powershell腳本,我需要從python腳本執行。 powershell腳本工作正常,當我從命令行運行它,但失敗時,我從python運行它。從使用網絡管理模塊的python運行powershell腳本

我已經簡化了我的powershell腳本以下代碼,它重現了這個問題。

PowerShell腳本:

import-module WebAdministration 
Add-Type -Path C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll 
Set-WebConfigurationProperty '/system.webServer/proxy' -Name "enabled" -Value "true" 

的Python腳本:

import subprocess 
print subprocess.check_output("powershell [pathToMyPowershellScript]"); 

當我運行cmd.exe的從PowerShell腳本,它工作正常,不產生輸出。 當我運行python腳本,它產生以下錯誤:

Set-WebConfigurationProperty : Retrieving the COM class factory for component 
with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following 
error: 80040154 
Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). 
At [pathToMyPowershellScript]:3 char:1 
+ Set-WebConfigurationProperty '/system.webServer/proxy' -Name "enabled" -Value "t ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   
: NotSpecified: (:) [Set-WebConfigurationProperty], COMException 
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException, 
    Microsoft.IIs.PowerShell.Provider.SetConfigurationPropertyCommand 

我運行IIS的Windows 7專業版(64位)7.5,與Python 2.7和PowerShell 1.0 任何想法?

+0

同樣發生在我從ruby運行powershell腳本時 – zcotter 2013-03-18 19:39:01

回答

2

我能夠從CMD的 「sysnative」 版本中運行PowerShell腳本來解決這個問題:

subprocess.check_call("c:\\windows\\sysnative\\cmd.exe /c 
         powershell pathToScript", shell=True) 

這解決了問題位數。

+0

我一直在尋找一個解決方案的年齡,這做了工作! – 2015-01-20 09:05:45

相關問題