System.Management.Automation 与 .NET Core 3.1 不兼容

Ale*_*x_P 4 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)
Run Code Online (Sandbox Code Playgroud)

我对受支持的.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]
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何System.Management.Automation在 .NET Core(跨平台项目)中使用来编写 PowerShell 函数?

mkl*_*nt0 5

不幸的是,尽管System.Management.Automation包的NuGet 页面没有提及它,但该包并不打算直接使用。

相反,根据您的用例,使用此答案中描述的软件包之一。

要以PowerShell (Core) 7+为目标,请使用Microsoft.PowerShell.SDK包。

  • 要确定给定的包版本必须至少与哪个特定的 .NET (Core) 运行时结合,请在链接页面上选择感兴趣的包版本,展开该Dependencies部分,然后查阅第一个条目;例如,对于包版本7.1.3,最低要求的 .NET (Core) 运行时是net5.0