locked
Importing WebAdministration Module in Powershell Host Application RRS feed

  • Question

  • User-2055844740 posted

    I am new to Powershell, and am not well versed in Windows administration.  I'd like to be able to run some simple Powershell scripts for managing an IIS 7.5 FTP site from within a VB.NET application. 

    I've created a host, and attempted to invoke the commands using the PowerShell class. 

    I am getting the message "file ...WebAdministrationAliases.ps1 cannot be loaded because the execution of scripts is disabled on this system..." message when I attempt to load the WebAdministration Module.

    A few additional notes:

    • I am running on Win2008R2 64bit, IIS7.5, Powershell 2, Visual Studio 2010 Beta 2.
    • I am running as administrator
    • I can run the commands from within Powershell just fine, assuming I run the Set-ExecutionPolicy and Import-Module commands first.
    • I've tried loading the module via both the InitialSessionState.ImportPSModule method, and the AddCommand("Import-Module WebAdministration").
    • I've tried passing Set-ExecutionPolicy Unrestricted via the AddCommand method, but it returns an error message stating that "The term 'Set-ExecutionPolicy Unrestricted' is not recognized as the name of a cmdlet, function, script file, or operable program..."
    • My application is signed.

    I'm sure I'm missing something simple, but I'm at a loss for what to try next to give this application the appropriate permissions.  Any help would be appreciated.

    Tuesday, December 8, 2009 4:58 PM

All replies

  • User744767459 posted

    Hi,

    Please try add the cmdlet to the pipeline, then add the arguments for the cmdlet immediately after the cmdlet is added. It looks like:

    AddCommand("Set-ExecutionPolicy")
    AddArgument("Unrestricted")

    Thursday, December 10, 2009 4:57 AM
  • User-2055844740 posted

    Thank you, that did seem to get me farther along.  Now I am running into the same issue listed in this thread:

     http://forums.iis.net/t/1161177.aspx

    My current image of 2008R2 had the Beta products for Office and SharePoint installed, as well as the Beta2 version of VS 2010.  I'm rolling back to an image prior to the Beta installs, and am going to try with Visual Studio 2008.

    Thursday, December 10, 2009 11:12 AM
  • User-2055844740 posted

    I tried the same code on a clean Win2008R2 system using Visual Studio 2008 SP1, and got the same result.  I am still getting the error "Cannot retrieve the dynamic paramters for the cmdlet.  Retrieving the COM class factory for component with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: 80040154".  I've checked the registry, and that CLSID is registered, and the .dll it is pointing to is on the system.

    At this point I'm losing confidence in the PowerShell approach.  I simply want to create a virtual directory in IIS 7.5 via .NET code.  If I understand correctly, using Active Directory no longer works with IIS 7.5 for this.  What are the alternative techniques for this, can anyone point me in the right direction?

    Friday, December 11, 2009 11:29 AM
  • User153071676 posted
    I have the same issue. I want to create a new IIS site as part of my automation testing but cant get Powershell to load the WebAdministration module from C#.
    Sunday, December 13, 2009 8:00 PM
  • User-2055844740 posted

    I was not able to resolve the issues I was having with the powershell approach.  I've switched and am now using the ServerManager class that is part of the Microsoft.Web.Administration dll.  This is working so far, and is probably a cleaner solution for what I was trying to do anyway.  Specifically, the other thread I referenced earlier and the post below led me down the path I'm using now.

     http://blogs.msdn.com/carlosag/archive/2006/04/17/MicrosoftWebAdministration.aspx

    Wednesday, December 16, 2009 11:06 AM
  • User-744658642 posted

    I figured out the issue...

    Cannot retrieve the dynamic parameters for the cmdlet. 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)).

    The C# .Net application is hosting Powershell and running a powershell script.  The script is calling a WebAdministration cmdlet.  The cmdlet call causes Powershell to load Microsoft.AppHostQueryProcessor (AppHostNavigators.dll).  This will fail when the .Net program's platform target doesn't match the machine's CPU.   In my case, the Platform target was "x86".  When run on an x64 Windows 2008 R2 server it can't find the x64 dll.

    The fix is to make sure .Net application is built with the correct Platform target ("Any CPU" is the best choice in most situations).

     

    Wednesday, April 20, 2011 4:07 PM
  • User-923906747 posted

    For those of you experiencing this issue just trying to run some WebAdministration code in a regular powershell window like I was, the following fix worked for me.  It is essentially the same logic as the previous reply but in a somewhat different context.  Running 64bit powershell has a problem executing 32bit COM objects like WebAdministration I guess.  So, open up a powershell window from here instead:

     C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

     I also set my execution policy to unrestricted, though I'm not sure if that was necessary. 

     Now I can run WebAdministration commands against a remote server.

    Wednesday, September 28, 2011 11:37 AM