1

I have a very long powershell script that I need to execute from a python script. The powershell script works fine when I run it from command line, but fails when I run it from python.

I've simplified my powershell script to the following code, which reproduces the issue.

Powershell Script:

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:

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

When I run the powershell script from cmd.exe, it works fine and produces no output. When I run the python script, it produces the following error:

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?

1
  • Also happens when I run the powershell script from ruby Commented Mar 18, 2013 at 19:39

2 Answers 2

2

I was able to fix this by running the powershell script from the "sysnative" version of cmd:

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

This solves the bitness issue.

Sign up to request clarification or add additional context in comments.

1 Comment

I've been looking for ages for a solution, This did the work!!
1

Your program may be calling the wrong version of powershell, try explictly calling the exe.

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

Heres some related reading you might find interesting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.