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!

4
  • does it work if you run the application as a local Administrator? Commented Jun 5, 2012 at 23:51
  • does this link help? stackoverflow.com/questions/2094694/… Commented Jun 6, 2012 at 0: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 Commented Jun 6, 2012 at 0:15
  • Please post your solution as answer and accept that. It helps to close the question. Commented Jun 10, 2012 at 9:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.