locked
Can't use IIS snapin when using powershell from c# RRS feed

  • Question

  • User1202642738 posted

    Trying to execute a powershell script that sets up a website. I have it working in the powershell ISE but can't get it to work when using it from c#.

    Error I get is:

    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)).

     That CLSID points to: Microsoft.AppHostQueryProcessor

    My code is:

    Collection<PSObject> results = new Collection<PSObject>();

    Runspace runspace = RunspaceFactory.CreateRunspace();

    try {

    runspace.Open();

    Pipeline pipeline = runspace.CreatePipeline();

    pipeline.Commands.AddScript(script); 

    pipeline.Commands.Add("Out-String");

    results = pipeline.Invoke();

     etc etc

    The script itself is:

    string script = new StringBuilder("Invoke-Command ")

    .Append(" -scriptblock {")

    .AppendLine()

    .AppendLine("Set-ExecutionPolicy RemoteSigned")

    .AppendLine("Add-PSSnapin WebAdministration") .AppendLine("import-module WebAdministration")

    //.AppendLine(@"& '$env:programfiles\IIS\PowerShellSnapin\iisConsole.psc1'")

    .AppendLine("IIS:")

    .AppendLine("get-Command Out-File | Format-List | out-string")

    .AppendLine("Get-Website") .AppendLine("}")

    .ToString();

    The script is not the actual script to create the website, but just a couple of test statements to figure out why it is failing on a simple Get-Website command. Seems any IIS Snapin command just fails. Currently this code runs from a unit test (MSTest).  

    I need to use the invoke-command because eventually it needs to create a website remotly.

     

    Thursday, September 24, 2009 3:50 PM

Answers

  • User1331732461 posted

    Hi,

    I can't reproduce the problem.
    And I don't see the product id of "Microsoft.AppHostQueryProcessor" is used by IIS Powershell on RTW (Go Live) build.
    Are you using old version? Or, please try your program in a clean machine with the IIS Powershell RTW (Go Live) build.

    I executed the below program and I did not see any error as the below screenshot:

    C:\temp>ConsoleApplication2.exe

    Name             ID   State      Physical Path                  Bindings

    ----             --   -----      -------------                  --------

    Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:


    FYI, here I am attaching the source files for the above program.

    test.ps1 souce file:
    ### supposing the powershell execution policy is already set with remotesigned
    Remove-PSSnapin webadministration
    Add-PSSnapin webadministration
    Get-WebSite

    program.cs source file:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Collections.ObjectModel;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;


    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string cmdLine = @"c:\temp\test.ps1";
                Console.WriteLine(Program.RunPSScript(cmdLine));
            }

            public static string RunPSScript(string scriptText)
            {
                // create Powershell runspace
                PSConsoleLoadException warnings = new PSConsoleLoadException();
                RunspaceConfiguration rsc = RunspaceConfiguration.Create();
                Runspace runspace = RunspaceFactory.CreateRunspace(rsc);

                // open it
                runspace.Open();

                // create a pipeline and feed it the script text
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(scriptText);

                // add an extra command to transform the script
                // output objects into nicely formatted strings
                pipeline.Commands.Add("Out-String");

                // execute the script
                Collection<PSObject> results = pipeline.Invoke();

                // close the runspace
                runspace.Close();

                // convert the script result into a single string
                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject obj in results)
                {
                    stringBuilder.AppendLine(obj.ToString());
                }
                return stringBuilder.ToString();
            }
        }
    }

    Thanks,

    Jeong Hwan Kim

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Thursday, September 24, 2009 6:04 PM
  • User1202642738 posted

    I only use the ISE for testing the script. That all works. However when running the script from c# using the powershell host, I get the COM error.

    I switched to using the servermanager inside a powershell script and that all works from c#.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, September 30, 2009 11:04 AM

All replies

  • User1331732461 posted

    Hi,

    I can't reproduce the problem.
    And I don't see the product id of "Microsoft.AppHostQueryProcessor" is used by IIS Powershell on RTW (Go Live) build.
    Are you using old version? Or, please try your program in a clean machine with the IIS Powershell RTW (Go Live) build.

    I executed the below program and I did not see any error as the below screenshot:

    C:\temp>ConsoleApplication2.exe

    Name             ID   State      Physical Path                  Bindings

    ----             --   -----      -------------                  --------

    Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:


    FYI, here I am attaching the source files for the above program.

    test.ps1 souce file:
    ### supposing the powershell execution policy is already set with remotesigned
    Remove-PSSnapin webadministration
    Add-PSSnapin webadministration
    Get-WebSite

    program.cs source file:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Collections.ObjectModel;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;


    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string cmdLine = @"c:\temp\test.ps1";
                Console.WriteLine(Program.RunPSScript(cmdLine));
            }

            public static string RunPSScript(string scriptText)
            {
                // create Powershell runspace
                PSConsoleLoadException warnings = new PSConsoleLoadException();
                RunspaceConfiguration rsc = RunspaceConfiguration.Create();
                Runspace runspace = RunspaceFactory.CreateRunspace(rsc);

                // open it
                runspace.Open();

                // create a pipeline and feed it the script text
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(scriptText);

                // add an extra command to transform the script
                // output objects into nicely formatted strings
                pipeline.Commands.Add("Out-String");

                // execute the script
                Collection<PSObject> results = pipeline.Invoke();

                // close the runspace
                runspace.Close();

                // convert the script result into a single string
                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject obj in results)
                {
                    stringBuilder.AppendLine(obj.ToString());
                }
                return stringBuilder.ToString();
            }
        }
    }

    Thanks,

    Jeong Hwan Kim

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Thursday, September 24, 2009 6:04 PM
  • User1202642738 posted

    Thanks Jeong.

    I am using Windows 7. That should have the latest powershell. I am however running Visual Studio 2010 Beta.

    I ran your test and it errors with:

    The term 'Get-WebSite' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 

    Not sure why that is. Very wierd. Could you try it with the command-invoke? See my previous example.

    the version for Microsoft.Web.Administration is 7.0.0.0

     

    Thursday, September 24, 2009 6:45 PM
  • User1331732461 posted

    I tried my program on Win7 and it worked well.

    BTW, I think you should check why the get-website is not recognized as the name of a cmdlet because the Get-WebSite is a valid cmdlet name on Win7 build.

    Maybe you can follow this on DOS prompt if you are not familiar about how to start IIS Powershell provider:

    C:\temp>powershell.exe
    Windows PowerShell
    Copyright (C) 2009 Microsoft Corporation. All rights reserved.

    PS C:\temp> Set-ExecutionPolicy RemoteSigned
    PS C:\temp> Import-module webadministration
    PS C:\temp> Get-WebSite

    Name             ID   State      Physical Path                  Bindings
    ----             --   -----      -------------                  --------
    Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:

    If the get-website does not work as the above result, your machine seems not a clean machine for some reasons and you would need to try other machines.

     

     

     

    Thursday, September 24, 2009 7:30 PM
  • User1331732461 posted

    BTW, I was wrong Microsoft.AppHostQueryProcessor is not related to IIS Powershell. Actually it is one of IIS component files in Win7.

    Please check the COM file is registered in your Win7 machine by searching the string "Microsoft.AppHostQueryProcessor" or class id "688eeee5-6a7e-422f-b2e1-6af00dc944a6" using RegEdit's Edit\Find menu.

     

     

    Thursday, September 24, 2009 7:46 PM
  • User1202642738 posted

    Yeah, I tried Powershell ISE first before using it in the c# program and it all works in there. I tried it again and it still works in the ISE.

    I put the Import-module webadministration in the script instead of the Add-PSSnapin and then I get the missing CLSID error again.

    If I use SystemManager (in the .Net framework) to create the webset remotely or locally in a powershell inside a c# program, than that works.

    Just the IIS snapin refuses to work inside the c# program.

    Thursday, September 24, 2009 9:08 PM
  • User1202642738 posted

    Thanks Jeong. I am impressed with the fast reponses. 

    The CLSID: Yeah, that was the first thing I did :-). It is there. I think I'll try it on a team members PC tomorrow. At the very least the SystemManager approach works

    Thursday, September 24, 2009 9:11 PM
  • User1331732461 posted

    FYI,

    I'd like to imform that Add-PSSnapin cmdlet should not be used if your machine is installed with Win7 OS. Only Import-module cmdlet should be used in that case. :-)

    Friday, September 25, 2009 1:48 PM
  • User2065605267 posted

    Trying to execute a powershell script that sets up a website. I have it working in the powershell ISE but can't get it to work when using it from c#.

    Error I get is:

    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)).

    Just to be clear, you can get your script to run in the ISE *and* from powershell.exe (just a regular console)?  You mention it failing from C#.

    I doubt this kind of COM error would result, but the ISE runs in STA mode exclusively, but powershell.exe runs in MTA mode by default.

    If your code fails in a regular powershell.exe session, try starting a new session by passing the "-sta" flag:

    PS>powershell.exe -sta

    Wednesday, September 30, 2009 10:50 AM
  • User1202642738 posted

    I only use the ISE for testing the script. That all works. However when running the script from c# using the powershell host, I get the COM error.

    I switched to using the servermanager inside a powershell script and that all works from c#.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, September 30, 2009 11:04 AM