简体   繁体   English

在VS2010中执行Powershell Invoke时出错

[英]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: 我正在尝试从类中执行powershell脚本,但是当我在x86模式下设置平台目标时出现错误:

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 我有从x86 Powershell文件夹获得的System.Management.Automation dll,所以我不知道为什么不在x86中工作

If I set the platform target to x64 it works ok 如果我将平台目标设置为x64,则可以正常工作

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. 关于这篇文章,在x86的Platform Target下从.NET运行PowerShell时,似乎有某些命令/模块似乎有问题。 I particularly had issues with "Import-Module WebAdministration" and would receive the same error that you did. 我特别遇到“导入模块Web管理”的问题,并且会收到与您相同的错误。 It did not matter where I got the System.Management.Automation.dll either. 无论我在哪里获得System.Management.Automation.dll都没关系。 Additionally, looking at the dll using ILSpy , both "visually" appear to be the same. 另外,使用ILSpy查看dll,两者“视觉上”看起来都是一样的。

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 . 关于这个完全相同或相似类型问题的帖子有几篇,所有这些都指向在x86目标平台上运行(我相信它可能需要将.NET应用程序神奇地重定向到PowerShell的SySWoW64版本。 这是一个例子与文件系统

While I know this isn't a great answer, I believe the answer to be that this cannot be done on the x86 platform. 虽然我知道这不是一个好的答案,但我相信答案是在x86平台上无法做到这一点。 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. 如果您尝试使用WebAdministration模块执行操作并且无法解决此错误,则可以尝试使用Matt Wrock的导入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". 最终,我对该项目的解决方案是将框架定位到“任何CPU”并将32位计算机视为“不受支持”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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