1

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

Python 2.7 と Powershell 1.0 を使用して、Windows 7 Professional (x64) で IIS 7.5 を実行しています。

4

2 に答える 2

2

「sysnative」バージョンのcmdからpowershellスクリプトを実行することで、これを修正できました。

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

これにより、ビット数の問題が解決されます。

于 2013-03-29T17:17:24.570 に答える
1

プログラムが間違ったバージョンの powershell を呼び出している可能性があります。明示的に exe を呼び出してみてください。

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

興味深いと思われる関連資料をいくつか紹介します。

于 2013-03-18T23:52:29.057 に答える