Cannot get powershell script to import module via msbuild

2

I have a post build step in an msbuild config which executes a powershell script. The powershell script works perfectly if I call it directly from Powershell but fails via msbuild.

It seems it fails after it tries to use classes from the import: Import-Module WebAdministration

I get the error "Error : 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))"

I've tried changing the version of powershell loaded by msbuild from 64bit to 32bit but it makes no difference.

Here is the msbuild step:

<Target Name="DevPostBuild">
<PropertyGroup>
  <PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
  <ScriptLocation Condition=" '$(ScriptLocation)'=='' ">$(ProjectDir)DevPostBuild.ps1</ScriptLocation>
</PropertyGroup>
<Message Text="$(ScriptLocation)" />
<Exec Condition="Exists($(ScriptLocation))" Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; { &amp;'$(ScriptLocation)' } &quot;" />

I'm using VS2010 on a 64bit machine.

Thanks!

visual-studio-2010
powershell
msbuild
powershell-2.0
asked on Stack Overflow Aug 21, 2013 by smithy

2 Answers

0

Visual Studio 2010 is a 32bit process. Powershell will then run as a 32bit process. The WebAdministration module is 64bit only. Try using %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe for PowerShellExe.

answered on Stack Overflow Aug 21, 2013 by Eris
0

Same in Visual Studio 2017, using syswow64's powershell.exe gives no difference. I had similar challenge while controlling IIS from within MSBuild XML and go same error messages about clases not registered. The Stop-WebAppPool and Stop-Website are useless therefore in MSBuild XML, as all from WebAdministration module, not mentioning Stop-IISSite from IISAdministration module.

In my case I had to utilize old good AppCmd executable from IIS. Works smooth, no issues. I also had the case when application pools and web sites are already stopped so be aware I ignore any error codes.

<Exec Command="$(WinDir)\SysWOW64\inetsrv\appcmd.exe stop apppool /apppool.name:&quot;My Application Pool&quot;" IgnoreExitCode="true" />
<Exec Command="$(WinDir)\SysWOW64\inetsrv\appcmd.exe stop site /site.name:&quot;My Web Site&quot;" IgnoreExitCode="true" />
answered on Stack Overflow Aug 18, 2020 by Dawid Ireno

User contributions licensed under CC BY-SA 3.0