【问题标题】:Unable to execute Powershell from C# Application无法从 C# 应用程序执行 Powershell
【发布时间】:2015-01-02 20:42:54
【问题描述】:

我在 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();
        }
    }
}

有什么想法吗?我遇到的所有事情都只是说要添加&lt;Reference Include="System.Management.Automation /&gt;,但这只会产生相同的运行时错误。

【问题讨论】:

  • 尝试使用Fuslogvw.exe工具查看绑定问题错误信息,可能是key不兼容:msdn.microsoft.com/en-us/library/e74a18c4%28v=vs.110%29.aspx
  • @LeslieDavies 除非我误解了如何使用 fuslogvw,否则当我运行 fuslogvw 时,应用程序日志中不会出现任何内容。
  • 以管理员权限运行并进入设置并选择“记录所有绑定到磁盘”或“记录绑定失败到磁盘”
  • @LeslieDavies 哎呀没有选中“记录所有绑定到磁盘”,现在东西正在弹出。我现在比较一下钥匙,谢谢!

标签: c# powershell-3.0


【解决方案1】:

您的代码在我的电脑上运行良好。我不确定明显是什么问题。 但是hereherehere

他们解决了你的问题

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多