PowerShell Binary Module - Could not load Automation module in PS 5.1

Viewed 95

I'm working on a PS Binary module and I developed it using PS 7.1. I tried importing the dll into PS 5.1 and I got the following error:

ipmo : Could not load file or assembly 'System.Management.Automation, Version=6.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

My .csproj file is as follows:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AssemblyName>assemblyName</AssemblyName>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PowerShellStandard.Library" Version="5.1.0-preview-06">
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="System.Management.Automation" Version="6.0.4" />
  </ItemGroup>

</Project>

This project is created by using the PowerShell Module Template via dotnet cli: dotnet new psmodule.

1 Answers

You did it backwards. If you want a module to be compatible with both Windows PowerShell and PowerShell Core, you need to develop it against PowerShell 5.1, then use the Portability Analyzer to determine how portable your module is.

System.Management.Automation houses most, if not all, of the PowerShell specific constructs. If you build against PowerShell Core, PowerShell 5.1 is NOT going to have the same version of that assembly loaded into memory. Hence, it cannot find the assembly version it is looking for.

Here is some more information you may find useful on the subject.


Edit:

If your module is not compatible both ways, you can develop it for PowerShell 5.1 anyways and use the WindowsCompatibility module from PowerShell Core which should implicitly make use of constructs only available in Windows PowerShell. There are some caveats, the module must not use "live" objects (like CIM, remote connections, etc.) and the module will effectively remain "Windows-only" since you can't run PowerShell 5.1 on Linux or MacOS.

Related