Reputation: 2950
I want to write PowerShell in C#. There is a useful link from red-gate.com which describes the steps for the process. The author uses the .Net Framework as Class library project. I have no problems with that, however, I would like to write my functions also for .Net Core in order to make them also cross-platform functional.
I created a new project with the target framework netcoreapp3.1
. When I install System.Management.Automation 7.1.2
I get the error:
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)
I am confused with the supported .NETCoreApp v5.0
. With this link https://dotnet.microsoft.com/download there is .NET v5.0 and .NET Core 3.1. When I target net5.0
I get the error that the reference assemblies are not found but I installed the .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]
My question, how can I use System.Management.Automation
in a .NET Core (cross-platform project) to write PowerShell functions?
Upvotes: 7
Views: 7262
Reputation: 439912
Even though, unfortunately, the System.Management.Automation
package's NuGet page doesn't mention it, this package is not meant to be used directly.
Instead, use one of the packages described in this answer, depending on your use case.
To target PowerShell (Core) 7+, use the Microsoft.PowerShell.SDK
package.
Dependencies
section, and consult the first entry; e.g., for package version 7.1.3
the minimum required .NET (Core) runtime is net5.0
Upvotes: 7