I am trying to create a .net standard 2.0 library that calls PowerShell commands and scripts using PowerShell classes (System.Management.Automation). I want to use this .Net Standard library in .Net Framework (4.7.2) project.
I have already tried using Microsoft.PowerShell.SDK
and System.Management.Automation
NuGet package in .Net Standard project but I keep getting this error:
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)
Sample Code in .Net Standard 2.0 Library project:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
PowerShell posh = PowerShell.Create();
posh.Runspace = runspace;
How can I achieve this? Any help is appreciated.
If you want to target .NET Standard, you should reference the PowerShellStandard.Library
nuget package
PowerShell Standard exposes only the API surface that overlaps between PowerShell 5.1 (.NET Framework) and PowerShell Core (.NET Core), so it'll work regardless of which edition you compile against.
Recommended reading: PowerShell Standard Library: Build single module that works across Windows PowerShell and PowerShell Core