PowerShell二进制模块-无法在ps5.1中加载自动化模块



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

ipmo:无法加载文件或程序集System.Management。自动化,版本=6.0.4.0,文化=中性,PublicKeyToken=31bf3856ad364e35'或其依赖项之一。系统找不到指定的文件

我的.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>

这个项目是通过dotnet cli:dotnet new psmodule使用PowerShell Module Template创建的。

你做反了。如果你想要一个模块同时兼容Windows PowerShell和PowerShell Core,你需要针对PowerShell 5.1开发它,然后使用可移植性分析器来确定你的模块的可移植性。

System.Management.Automation包含了大部分(如果不是全部的话)PowerShell特定的结构。如果你是基于PowerShell Core构建的,PowerShell 5.1不会将相同版本的程序集加载到内存中。因此,它找不到它正在查找的程序集版本。

这里有一些关于这个主题的更多信息,你可能会觉得有用。

编辑:

如果你的模块不兼容两种方式,你可以为PowerShell 5.1开发它,并使用PowerShell Core的WindowsCompatibility模块,该模块应该隐式地使用仅在Windows PowerShell中可用的结构。有一些注意事项,模块一定不要使用"live";对象(如CIM、远程连接等),该模块将有效地保持"仅限windows";因为你不能在Linux或MacOS上运行PowerShell 5.1。

最新更新