从使用网络管理模块的python运行powershell脚本
问题描述:
我有一个很长的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 任何想法?
答
我能够从CMD的 “sysnative” 版本中运行PowerShell脚本来解决这个问题:
subprocess.check_call("c:\\windows\\sysnative\\cmd.exe /c
powershell pathToScript", shell=True)
这解决了问题位数。
+0
我一直在寻找一个解决方案的年龄,这做了工作! – 2015-01-20 09:05:45
答
你的程序可能会调用powershell的错误版本,请尝试明确地调用exe。
%SystemRoot%\SysWoW64\WindowsPowerShell\v1.0\powershell.exe
同样发生在我从ruby运行powershell脚本时 – zcotter 2013-03-18 19:39:01