Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"An error occurred when loading the system Windows PowerShell snap-ins"

Trying to execute powershell script in my C# code. Using .NET Framework v. 4.0.30319 SP1Rel in VS 2010 on Windows 7.

I have the following dll file as reference:

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

And I include:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

Then when I run the following commands:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); // here
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);

It crashes on the first line with the following exception:

{"An error occurred when loading the system Windows PowerShell snap-ins. Please contact Microsoft Support Services."}

Inner exception:

{"Unable to access Windows PowerShell PowerShellEngine registry information."}

I am able to run "Powershell.exe" (version 2 or 1) in cmd and it works fine and my project compiles as it should. I read this question (http://social.msdn.microsoft.com/Forums/eu/biztalkesb/thread/a807bee7-531a-4990-b8da-47a7e6e3e099) where they talked about some reg. keys but he forgot to mention exactly what he did to fix it. Really appreciate any help!

like image 995
chikuba Avatar asked Jun 05 '12 23:06

chikuba


1 Answers

I opened a terminal and ran powershell v2 and then I ran the following command:

"echo ([PSObject].Assembly.Location)"

which will give you the correct path to the dll being used.

like image 140
chikuba Avatar answered Nov 13 '22 18:11

chikuba