c# - System.Management.Automation 与 .NET Core 3.1 不兼容

标签 c# powershell

我想用 C# 编写 PowerShell。来自 red-gate.com 的有用链接它描述了该过程的步骤。作者使用 .Net Framework 作为 Class 库项目。我对此没有任何问题,但是,我还想为 .Net Core 编写我的函数,以使它们也具有跨平台功能。
我用目标框架创建了一个新项目 netcoreapp3.1 .当我安装 System.Management.Automation 7.1.2我收到错误:

Error   NU1202  Package System.Management.Automation 7.1.2 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Package System.Management.Automation 7.1.2 supports: net5.0 (.NETCoreApp,Version=v5.0)
我对支持的 .NETCoreApp v5.0 感到困惑.通过此链接 https://dotnet.microsoft.com/download有 .NET v5.0 和 .NET Core 3.1。当我瞄准 net5.0我收到未找到引用程序集的错误,但我安装了 .NET 5.0 SDK。
C:\Users\Alex_P>dotnet --list-sdks
3.1.403 [C:\Program Files\dotnet\sdk]
5.0.201 [C:\Program Files\dotnet\sdk]
我的问题,我该如何使用 System.Management.Automation在 .NET Core(跨平台项目)中编写 PowerShell 函数?

最佳答案

尽管不幸的是,System.Management.Automation包裹的NuGet page没有提到它,这个包不是直接使用的。
相反,使用 this answer 中描述的软件包之一。 ,取决于您的用例。
要针对 PowerShell (Core) 7+,请使用 Microsoft.PowerShell.SDK 包裹。

  • 要确定给定包版本必须至少与哪个特定的 .NET (Core) 运行时结合,请在链接页面上选择感兴趣的包版本,展开 Dependencies部分,并查阅第一个条目;例如,对于包版本 7.1.3最低要求的 .NET (Core) 运行时是 net5.0
  • 关于c# - System.Management.Automation 与 .NET Core 3.1 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66607550/

    相关文章:

    c# - 如何分隔 C# 元组值以匹配方法参数

    powershell - 使用 PowerShell 测试 FTP 连接

    html - 无法使用 PowerShell 获取元素文本

    powershell - 使用PowerShell通过管道将用户添加到组中

    c# - 匿名类型列表?

    c# - Q# 环境不一致

    c# - 是否有可能使枚举自动生成其值

    c# - 如果 Windows 版本低于 7 则退出

    powershell - 在资源管理器中使用 PowerShell 脚本打开文件

    使用 .NET ZipFile.CreateFromDirectory 时,Powershell 脚本使用用户的主目录作为当前工作目录。怎么修?