2

I have a PowerShell module written in C#. Recently I upgraded it to .NET 8. My project file looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <AssemblyName>MyAssemblyName</AssemblyName>
    <Nullable>enable</Nullable>
    <Version>0.0.0</Version>
    <AssemblyVersion>0.0.0</AssemblyVersion>
    <SelfContained>true</SelfContained>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="PowerShellStandard.Library" Version="5.1.0-preview-06">
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\Path\To\Domain\Project.csproj" />
  </ItemGroup>
</Project>

I publish it like this:

dotnet publish $projectFile --self-contained --configuration Release --output C:\Some\Path

The module works fine in PowerShell 7.4 but does not work in PowerShell 7.2. Import-Module always fails with this error:

Import-Module: Could not load file or assembly 'System.Console, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (0x80131621)

Is this expected? When I publish as self-contained, I expect it will work without a need to upgrade PowerShell. Is the expectation wrong?

4
  • This issue is expected, net8 didn't even exist in 7.2. If you want your module to be compatible with pwsh 7 LTS you should publish using net6. Example for multitarget project: github.com/santisq/PSADTree/blob/main/src/PSADTree/… Commented Apr 4, 2024 at 16:11
  • see: learn.microsoft.com/en-us/powershell/scripting/install/… Commented Apr 4, 2024 at 16:16
  • Thank you Santiago. Why I thought it should NOT matter that net8 didn't even exist: "Publishing your app as self-contained produces an application that includes the .NET runtime and libraries, and your application and its dependencies. Users of the application can run it on a machine that doesn't have the .NET runtime installed." (from MS docs). So I tried to build my PS module as self-contained and expected it to work. Commented Apr 5, 2024 at 13:30
  • 1
    Ah yeah, wouldn't work like that for a PowerShell module, I believe that description would be valid for, for example, a Console Application. PowerShell is already depending on a specific version of .NET. Basically the reason why you always want PrivateAssets="all" for the package reference of the powershell SDK. Commented Apr 5, 2024 at 13:32

1 Answer 1

0

This issue is expected, PowerShell 7.2 (LTS-previous) is based of .NET 6. If you want your module to be fully compatible with pwsh 7 users (v7.2+) you should publish targeting net6.0. See PowerShell End-of-support dates for reference.

There is also the possibility to have a multi-target project, see for example PSADTree.csproj which creates a specific assembly for Windows PowerShell 5.1 users and PowerShell 7+ users. The in your .psd1 you can use different conditions to determine which .dll should be used, example PSADTree.psd1#L12-L17:

RootModule = if ($PSEdition -eq 'Core') {
    'bin/net6.0/PSADTree.dll'
}
else {
    'bin/net472/PSADTree.dll'
}

Unfortunately, the .psd1 will not have access to $PSVersionTable if you desire to multitarget for net6, 7 & 8 separately. In that case you would define this logic in a .psm1.

Sign up to request clarification or add additional context in comments.

2 Comments

This looks cool, I never realized this is possible. In my case I'd check for $PSVersionTable.PSVersion.Major -eq 7 -and $PSVersionTable.PSVersion.Minor -eq 2 (direct that one to dll built with .NET 6) etc. But not going to do that as my goal is to upgrade my code to .NET 8. I was investigating if there is a way to run the upgraded module on PS versions lower than 7.4. Build it as self-contained is nonsense, right?
@Petr as explained in the comment a few minutes ago, you might have it working but might fail very easily. Try using <PackageReference Include="System.Management.Automation" Version="7.2.0" PrivateAssets="all" /> instead of <PackageReference Include="PowerShellStandard.Library" Version="5.1.0-preview-06">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.