6

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!

chikuba
  • 4,229
  • 6
  • 43
  • 75
  • does it work if you run the application as a local Administrator? – Dan Jun 05 '12 at 23:51
  • does this link help? http://stackoverflow.com/questions/2094694/how-can-i-run-powershell-with-the-net-4-runtime – Dan Jun 06 '12 at 00:06
  • found the issue. Was referening to the wrong dll. file and the correct one can be found running "echo ([PSObject].Assembly.Location)" from powershell v.2 – chikuba Jun 06 '12 at 00:15
  • Please post your solution as answer and accept that. It helps to close the question. – RinoTom Jun 10 '12 at 09:27

1 Answers1

3

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.

chikuba
  • 4,229
  • 6
  • 43
  • 75