简体   繁体   English

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

[英]Run a powershell script from python that uses Web-Administration module

I have a very long powershell script that I need to execute from a python script. 我有一个很长的PowerShell脚本,我需要从python脚本执行。 The powershell script works fine when I run it from command line, but fails when I run it from python. 当我从命令行运行powershell脚本时,powershell脚本工作正常,但是当我从python运行它时失败。

I've simplified my powershell script to the following code, which reproduces the issue. 我已经将我的powershell脚本简化为以下代码,它重现了这个问题。

Powershell Script: 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 Script: Python脚本:

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

When I run the powershell script from cmd.exe, it works fine and produces no output. 当我从cmd.exe运行powershell脚本时,它工作正常,不产生任何输出。 When I run the python script, it produces the following error: 当我运行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

I'm running IIS 7.5 on Windows 7 Professional (x64), with Python 2.7 and Powershell 1.0 Any ideas? 我在Windows 7 Professional(x64)上运行IIS 7.5,使用Python 2.7和Powershell 1.0任何想法?

I was able to fix this by running the powershell script from the "sysnative" version of cmd: 我能够通过从cmd的“sysnative”版本运行powershell脚本来解决这个问题:

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

This solves the bitness issue. 这解决了比特问题。

Your program may be calling the wrong version of powershell, try explictly calling the exe. 你的程序可能正在调用错误版本的powershell,试着明确地调用exe。

%SystemRoot%\SysWoW64\WindowsPowerShell\v1.0\powershell.exe

Heres some related reading you might find interesting. 你可能会发现一些有趣的相关阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM