0

I'm trying an experiment and I want to create a CmdLet for PowerShell in NET8. I have created a new Class Library and added the following packages:

  • Microsoft.PowerShell.SDK
  • PowerShellStandard.Library

as suggested in this post. Then, I added this code to create the CmdLet:

using System.Management.Automation;

namespace PowerShellCmdLet
{
    [Cmdlet("Get", "Test")]
    public class TestClass : Cmdlet
    {
        protected override void BeginProcessing()
        {
            Console.WriteLine("BeginProcessing");
        }
    }
}

When I open PowerShell on the project bin directory, I import my dll with

Import-Module .\PowerShellCmdLet.dll

but then I get this error:

Import-Module : Could not load file or assembly 'System.Management.Automation, Version=7.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. At line:1 char:1 Import-Module .\PowerShellCmdLet.dll

CategoryInfo          : NotSpecified: (:) [Import-Module], FileNotFoundException
FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand

I tried to open PowerShell as an administrator but I get the same issue.

8
  • When you say you "open PowerShell", you're talking about powershell.exe, not pwsh.exe? powershell.exe only hosts Windows PowerShell 5.1, which is built on .NET Framework. It'll never be able to load your .NET 8 assembly. Commented Jan 30, 2024 at 12:25
  • I tried both and I get the same error Commented Jan 30, 2024 at 12:30
  • And what version of pwsh.exe are you running? "$($PSVersionTable.PSVersion)" Commented Jan 30, 2024 at 12:32
  • The powershell version should match the System.Management.Automation version. So you should have atleast 7.4 installed. Commented Jan 30, 2024 at 12:38
  • Does this answer your question? Powershell and .NET5.0 fails loading the assembly Commented Jan 30, 2024 at 15:39

1 Answer 1

0

i used the package: System.Management.Automation" Version="7.4.1" to build the dll.

<ItemGroup>
  <PackageReference Include="System.Management.Automation" Version="7.4.1" />
</ItemGroup>

Then using pwsh v 7.4.1, i can load the dll successfully and run the command

Import-Module path/to/PowerShellCmdLet.dll
#call the command
Get-Test

output:

BeginProcessing

Poweshell ISE can not load the dll because it does not support net8

If you are using VS code, you can run it, because vs code use pwsh if it is installed.

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

Comments

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.