简体   繁体   中英

Error executing Powershell Invoke in VS2010

I am trying to execute a powershell script from a class but I am getting an error when I set the platform target in x86 mode:

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

I have the System.Management.Automation dll that I got from the x86 Powershell folder, so I don't get why is not working in x86

If I set the platform target to x64 it works ok

Here is my code:

 RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
        runspace.Open();

        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

        Pipeline pipeline = runspace.CreatePipeline();

        //Here's how you add a new script with arguments
        string fullPath = Path.GetFullPath(@"..\..\Scripts\CreateApplicationPool.ps1");
        Command myCommand = new Command(fullPath);


        myCommand.Parameters.Add("ApplicationPoolName", "example");

        pipeline.Commands.Add(myCommand);

        // Execute PowerShell script
        Collection<PSObject> results = pipeline.Invoke();

I realize this is a new post to an old questions...came across it while searching for a solution myself.

In regards to this post, there appear to be certain commands/modules that seem to have issue when running PowerShell from .NET under the Platform Target of x86. I particularly had issues with "Import-Module WebAdministration" and would receive the same error that you did. It did not matter where I got the System.Management.Automation.dll either. Additionally, looking at the dll using ILSpy , both "visually" appear to be the same.

Unfortunately, I was not able to resolve this nor was I able to find any viable solution. There are several posts on this exact same or similar type problem and all point back to running on an x86 targeted platform (I believe it likely has to due with the magical redirection of .NET applications to the SySWoW64 version of PowerShell. Here's an example of that with the File System .

While I know this isn't a great answer, I believe the answer to be that this cannot be done on the x86 platform. If you are attempting to do things with the WebAdministration module and cannot get past this error, you could try Matt Wrock's solution of Importing the Microsoft.Web.Administration. This appears to be an acceptable work around.

Ultimately, my solution for the project that I am on was to target the framework to "Any CPU" and to consider 32bit machines as "unsupported".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM