PowerShell: Import-Module, but no "ExportedCommands" available
Asked Answered
E

4

9

When I use

Import-Module -Name <path_to_local_dll> -Verbose

the cmdlets contained in the DLL file are not exported.

Thus, when I type Get-Module my imported module is listed, but without any ExportedCommands. Why?

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Binary     MyModule

On a second PC with the same software (PowerShell, .NET Framework, ...), the same imported DLL file works fine. There I get ExportedCommands.

On what can this behaviour depend?

Unfortunately, the Import-Module cmdlet gives no indication that it failed to import the cmdlets. Is there a way to get an indication why it fails?

Elishaelision answered 12/10, 2015 at 13:24 Comment(5)
What version of .NET did you compile against and what version of System.Management.Automation.dll (1.0 or 3.0) does it reference? Also what version of PowerShell (and bitness) is the first machine running? Is your assembly compiled Any CPU?Chronological
I didn't compiled the dll. I just use it. Thus, I don't know the version of .NET it was compiled against and the version of System.Management.Automation.dll. I use 32 bit PowerShell 4.0. (PSVersion 4.0, WSManStackVersion 3.0, SerializationVersion 1.1.0.1, CLRVersion 4.0.30319.34209, BuildVersion 6.3.9600.16406, PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}, PSRemotingProtocolVersion 2.2)Elishaelision
Have you figured out the root cause of this problem? I'm having similar issue. Please let me know if you have a solution/workaround. Thanks !Decode
No, I didn't. As I had further problems based on this, I had to re-installed windows and all other stuff.Elishaelision
I was using a third-party tool (PS Protector) to convert *.psm1 to *.dll when I encountered this issue, despite having the *.psd1 file configured correctly. I actually was unable to locate the precise issue - it turned out that the problem was with the dll file, and generating the EXACT SAME dll with a slightly different name fixed the problem. Bizarre.Murmuration
P
12

Two things:

  1. Make sure you're using a module manifest file (.psd1 file). More information can be found in How to Write a Module Manifest

  2. Most importantly, edit your manifest file and make sure it references your root module as follows:

    RootModule = 'name of your module'

I just finished fighting with this for a few hours and I couldn't figure out what I was missing from my other modules. This did the trick for sure!

Purulence answered 25/1, 2018 at 20:41 Comment(2)
Is 'name of your module' with our without the ".psm1" extension? What does this power?Dupondius
Without the .psm1. I just use the same module name. I created one called "CxdCallData" my manifest looks like: RootModule = "CxdCallData".Purulence
L
1

One other requirement: ensure that the cmdlet class is public. For example, in my .cs file I initially had:

[Cmdlet(VerbsCommon.Get, "Proc")]
class GetProcCommand : Cmdlet
{ ...

Even after adding a manifest file with RootModule set, Get-Module continued to show no ExportedCommands after my Import-Module. To fix it I just marked the class as public and rebuilt my .dll assembly:

[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{ ...

I figured this out while examining my .dll using ildasm - I noticed that some of my classes were public, but my cmdlet class was private.

Leventhal answered 22/3, 2019 at 17:15 Comment(0)
B
0

It may be that the psd1 file (the module manifest) does not contain the commands.

This page has a tutorial on how to create a module manifest.

Boehike answered 16/12, 2016 at 4:7 Comment(0)
S
0

Explicitly exporting function from the PowerShell module worked for me:

function New-CmdLetNameHere
{
    ...
}
Export-ModuleMember -Function New-CmdLetNameHere
Sigma answered 14/8, 2017 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.