无法从 C# 应用程序执行 Powershell
本文关键字:执行 Powershell 应用程序 | 更新日期: 2023-09-27 18:36:03
我添加了对Powershell dll(C:''Program Files (x86)''Reference Assemblies''Microsoft''WindowsPowerShell''3.0''System.Management.Automation.dll)的引用;我还删除了该引用并打开了项目文件并添加了 <Reference Include="System.Management.Automation />
.但是,这些都不起作用,我总是收到以下错误:
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)
这是我的代码:
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();
}
}
}
有什么想法吗?我遇到的一切都只是说要添加<Reference Include="System.Management.Automation />
,但这只会产生相同的运行时错误。
你的代码在我的电脑上工作正常。我不确定显然是什么问题。但是在这里,这里和这里
他们解决您的问题