gpt4 book ai didi

python - 从使用 Web 管理模块的 python 运行 powershell 脚本

转载 作者:行者123 更新时间:2023-11-30 23:37:29 25 4
gpt4 key购买 nike

我有一个很长的 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 Professional (x64) 上运行 IIS 7.5,并使用 Python 2.7 和 Powershell 1.0有什么想法吗?

最佳答案

我能够通过从“sysnative”版本的 cmd 运行 powershell 脚本来修复此问题:

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

这解决了位数问题。

关于python - 从使用 Web 管理模块的 python 运行 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15485173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com