4

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.

1 Answer 1

6

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

1
  • Thanks Mathias. PowerShellStandard library worked fo both .Net Standard and .Net Framework project.
    – Mark
    Commented Jun 26, 2019 at 3:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.