Unable to execute Powershell from C# Application

0

I've added in the reference to the Powershell dll (C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll); I've also removed that reference and opened up the Project File and added in <Reference Include="System.Management.Automation />. However, neither of these work, I always get the following error:

Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Powershell_Execution
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (String str in PowerShell.Create().AddScript("Get-Process").AddCommand("Out-String").Invoke<String>())
            {
                Console.WriteLine(str);
            }



            Console.ReadLine();
        }
    }
}

Any ideas? Everything I've run across just said to add the <Reference Include="System.Management.Automation />, but that just produces the same runtime error.

c#
powershell-3.0
asked on Stack Overflow Jan 2, 2015 by Daniel Martin

1 Answer

0

Your code work fine on my PC. I'm not sure what the problem obviously. But here , here and here

They solve your problem

answered on Stack Overflow Jan 2, 2015 by Selçuk Güral • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0