PowerShell 二进制模块 - 无法在 PS 5.1 中加载自动化模块

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

我正在开发 PS 二进制模块,我使用 PS 7.1 开发了它。我尝试将 dll 导入 PS 5.1,但出现以下错误:

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.

我的.csproj文件如下:

<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>

此项目是使用 PowerShell Module Template 通过 dotnet cli 创建的:dotnet new psmodule.

你做反了。如果您希望模块与 Windows PowerShell 和 PowerShell Core 兼容,则需要针对 PowerShell 5.1 进行开发,然后使用可移植性分析器确定模块的可移植性。

System.Management.Automation 包含大部分(如果不是全部)PowerShell 特定构造。如果您针对 PowerShell Core 构建,PowerShell 5.1 不会将该程序集的相同版本加载到内存中。因此,它找不到它正在寻找的程序集版本。

Here is some more information 你可能会发现对这个主题有用。


编辑:

如果您的模块不兼容两种方式,您仍然可以为 PowerShell 5.1 开发它并使用 PowerShell Core 中的 WindowsCompatibility 模块,它应该隐式地使用仅在 Windows PowerShell 中可用的结构.有一些注意事项,该模块不得使用“实时”对象(如 CIM、远程连接等),并且该模块将有效地保持“Windows-only”,因为您不能 运行 PowerShell Linux 或 MacOS 上的 5.1。