【问题标题】:Can I use PowerShell class in .Net Standard library to use in .Net Framework project (exe)我可以在 .Net 标准库中使用 PowerShell 类在 .Net Framework 项目(exe)中使用吗
【发布时间】:2019-06-25 07:39:51
【问题描述】:

我正在尝试创建一个 .net 标准 2.0 库,该库使用 PowerShell 类 (System.Management.Automation) 调用 PowerShell 命令和脚本。我想在 .Net Framework (4.7.2) 项目中使用这个 .Net 标准库。

我已经尝试在 .Net Standard 项目中使用 Microsoft.PowerShell.SDKSystem.Management.Automation NuGet 包,但我不断收到此错误:

System.IO.FileLoadException

 HResult=0x80131040
 Message=Could not load file or assembly'System.Management.Automation, Version=6.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

.Net Standard 2.0 库项目中的示例代码:

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

PowerShell posh = PowerShell.Create();
posh.Runspace = runspace;

我怎样才能做到这一点?任何帮助表示赞赏。

【问题讨论】:

    标签: c# powershell .net-framework-version .net-standard-2.0


    【解决方案1】:

    如果你想以 .NET Standard 为目标,你应该引用 PowerShellStandard.Library nuget 包

    PowerShell Standard 仅公开在 PowerShell 5.1 (.NET Framework) 和 PowerShell Core (.NET Core) 之间重叠的 API 表面,因此无论您针对哪个版本进行编译,它都可以工作。

    推荐阅读:PowerShell Standard Library: Build single module that works across Windows PowerShell and PowerShell Core

    【讨论】:

    • 谢谢马蒂亚斯。 PowerShellStandard 库适用于 .Net Standard 和 .Net Framework 项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多