2
votes

I'd like export an alias from a binary module that I've created. For script modules, you'd use Export-ModuleMember. Is there an equivalent for binary modules?

My manifest (.psd1) looks something like this:

@{
    ModuleToProcess = 'MyModule.psm1'
    NestedModules = 'MyModule.dll'
    ModuleVersion = '1.0'
    GUID = 'bb0ae680-5c5f-414c-961a-dce366144546'
    Author = 'Me'
    CompanyName = 'ACME'
    Copyright = '© ACME'
} 

EDIT: Keith Hill provided some help, but still to no avail. Here are all the files involved

My module script (.psm1):

export-modulemember -function Get-TestCommand -alias gtc

and finally, the code in my DLL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;

namespace MyModule
{
    [Cmdlet(VerbsCommon.Get, "TestCommand")]
    [OutputType(typeof(string))]
    public class GetTestCommand : PSCmdlet
    {
        protected override void ProcessRecord()
        {
            WriteObject("One");
            WriteObject("Two");
            WriteObject("Three");
        }
    }
}

If I have this and fire up PowerShell, then import-module MyModule and finally run get-module, I get this:

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Script     MyModule                  {}

If I comment out the export-modulemember bit in the psm1 file and repeat the above steps, I get this:

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Script     MyModule                  Get-TestCommand

So, what am I doing wrong here?

2
The output of Get-Module doesn't show aliases by default. Try this: Get-Module MyModule | Foreach {$_.ExportedAliases} or simply gmo MyModule | fl. - Keith Hill
It's the missing ExportedCommands that are concerning me, not the aliases - His Royal Redness
Where is your CmdletsToExport setting in the PSD1 file? Also don't export the cmdlet from the PSM1 file using Export-ModuleMember. - Keith Hill

2 Answers

2
votes

The typical way to do this is to make a .PSM1 your ModuleToProcess and put the Set-Alias / Export-ModuleMember -Alias * in that PSM1 file. Then put your DLL reference into the PSD1's NestedModules member e.g.:

@{
    ModuleToProcess = 'MyModule.psm1'
    NestedModules   = 'MyModule.dll'
    ModuleVersion = '1.0'
    GUID = 'bb0ae680-5c5f-414c-961a-dce366144546'
    Author = 'Me'
    CompanyName = 'ACME'
    Copyright = '© ACME'
    FormatsToProcess = 'MyModule.format.ps1xml'
    AliasesToExport = '*'
    CmdletsToExport = @(
        'Get-Foo',
        'Set-Foo',
        ...
    )
} 
1
votes

OK, I've got it working now. It turns out there were a few mistakes that I made, all of which contributed to the problem.

  • Problem 1: I don't understand PowerShell nearly as well as I'd like!
  • Problem 2: I was confusing Functions and Cmdlets. I needed to specify Export-ModuleMember -Cmdlet Get-TestCommand, rather than Export-ModuleMember -Function Get-TestCommand. This explains why my ExportedCommand disappeared everytime I uncommented the Export-ModuleMember line. Keith Hill's links to the PSCE files helped me discover that.

    Functionality in a DLL = -Cmdlet
    Functionality in .psm1 = -Function
    
  • Problem 3: It doesn't help exporting aliases if you haven't defined any. You need to set the aliases with Set-Alias first, then export it with Export-Module. This was a silly mistake on my part

So , in the end, changing my .psm1 file to the one shown below fixed the problem

Set-Alias gtc Get-TestCommand
Export-ModuleMember -Alias * -Function * -Cmdlet *

I'm going to give Keith credit for the answer. It's only because of his effort that I was able to get this going