【问题标题】:How to reference System.Management.Automation in a .NET Framework 4.7.2?如何在 .NET Framework 4.7.2 中引用 System.Management.Automation?
【发布时间】:2020-04-03 17:17:04
【问题描述】:

我开始了一个新的 .NET Framework 4.7.2 库项目。我需要自动化 PowerShell 脚本,但 Visual Studio 的参考添加 UI 中的“框架”选项卡没有将 System.Management.Automation 列为选项。所以我添加了对这个 Nuget 包的引用:

https://www.nuget.org/packages/System.Management.Automation/7.0.0

然后用这个代码:

PowerShell ps = PowerShell.Create();
ps.AddScript(@"C:\ps\function.ps1");
ps.AddArgument(1);
ps.AddArgument(2);
Collection<PSObject> results = ps.Invoke<PSObject>();

我收到关于我没有直接引用的 DLL 版本的错误:

具有标识 'System.Management.Automation, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 的程序集 'System.Management.Automation' 使用 'System.Linq.Expressions, Version=4.2.2.0, Culture=中性,PublicKeyToken=b03f5f7f11d50a3a',其版本高于引用的程序集 'System.Linq.Expressions',标识为 'System.Linq.Expressions, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

我不确定如何解决这个问题,我想我可能以错误的方式添加了我的 PowerShell 自动化库引用。目前这样做的正确方法是什么?

在您指出一个旧答案之前,我发现了一个类似的问题,它指向一个不同的 Nuget 包,现在标记为“已弃用”并且看起来并不正式。这让我很紧张。

https://www.nuget.org/packages/System.Management.Automation.dll/

【问题讨论】:

标签: c# .net powershell


【解决方案1】:

System.Management.Automation v7.0.0 仅适用于 .NET Core。如果您的项目需要 .NET Framework,则必须使用 System.Management.Automation v5.1.x。

【讨论】:

  • 谢谢 - 那么我在哪里可以得到那个版本呢?似乎无法从上面链接的同一个 nuget 集合中获得。
  • 尝试安装 5.1 版的 nuget 包,但它与我的框架版本 4.6 不兼容,所以我使用了上面建议的 @SteveLee。
【解决方案2】:

我在我的.net framework 4.7.2 应用程序中使用System.Management.Automation.dll。 我只是通过以下参考添加它:

C:\WINDOWS\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2021-12-25
    • 2022-12-14
    • 2022-12-04
    相关资源
    最近更新 更多