从Python运行使用Web-Administration模块的PowerShell脚本
我有一个很长的PowerShell脚本,我需要从一个Python脚本中执行它。这个PowerShell脚本在命令行中运行得很好,但在Python中运行时却失败了。
我把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
我在Windows 7专业版(x64)上运行IIS 7.5,使用的是Python 2.7和PowerShell 1.0。有什么想法吗?
2 个回答
1
你的程序可能在调用错误版本的PowerShell,试着直接调用它的.exe文件。
%SystemRoot%\SysWoW64\WindowsPowerShell\v1.0\powershell.exe
2
我通过从“sysnative”版本的命令提示符运行这个powershell脚本来解决了这个问题:
subprocess.check_call("c:\\windows\\sysnative\\cmd.exe /c
powershell pathToScript", shell=True)
这样就解决了位数的问题。