"加载系统Windows PowerShell管理单元时发生错误"

chi*_*uba 6 c# powershell windows-7

尝试在我的C#代码中执行powershell脚本.在Windows 7上的VS 2010中使用.NET Framework v.4.0.30319 SP1Rel.

我有以下dll文件作为参考:

C:\ Program Files(x86)\ Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\system.management.automation.dll

我包括:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
Run Code Online (Sandbox Code Playgroud)

然后当我运行以下命令时:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); // here
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
Run Code Online (Sandbox Code Playgroud)

它在第一行崩溃,但有以下异常:

{"An error occurred when loading the system Windows PowerShell snap-ins. Please contact Microsoft Support Services."}
Run Code Online (Sandbox Code Playgroud)

内部异常:

{"Unable to access Windows PowerShell PowerShellEngine registry information."}
Run Code Online (Sandbox Code Playgroud)

我能够在cmd中运行"Powershell.exe"(版本2或1),它工作正常,我的项目编译应该.我读了这个问题(http://social.msdn.microsoft.com/Forums/eu/biztalkesb/thread/a807bee7-531a-4990-b8da-47a7e6e3e099),在那里他们谈到了一些注册.钥匙,但他忘了提到他做了什么来解决它.非常感谢任何帮助!

chi*_*uba 3

我打开终端并运行 powershell v2,然后运行以下命令:

"echo ([PSObject].Assembly.Location)"
Run Code Online (Sandbox Code Playgroud)

这将为您提供正在使用的 dll 的正确路径。