簡體   English   中英

從python運行使用Web管理模塊的powershell腳本

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

我有一個很長的PowerShell腳本,我需要從python腳本執行。 當我從命令行運行powershell腳本時,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任何想法?

我能夠通過從cmd的“sysnative”版本運行powershell腳本來解決這個問題:

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

這解決了比特問題。

你的程序可能正在調用錯誤版本的powershell,試着明確地調用exe。

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

你可能會發現一些有趣的相關閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM