简体   繁体   中英

Unable to execute Powershell from C# Application

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.

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

They solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM