How to export PowerShell module aliases with a module manifest?

I have a module with several functions.

Since I called them the non-PowerShell way, I want to rename them. But since the module is already in use, I want to keep the old function names.

The best way to achieve this seems to be using aliases. I already have a module manifest that states:

AliasesToExport = '*' 

So, I created an alias in the module with New-Alias -Name test -Value oldFunctionName .

Functions were imported as usual, but there was no alias.

I know that I can use the Export-ModuleMember module in the module. But I have a manifest that should already take care of this.

So here are finally my questions:

Why aren't aliases exported through the manifest?

Is there a special place in the function itself where I can or should define an alias? Or do I need to use the New-Alias โ€‹โ€‹cmdlet somewhere special?

I was thinking of something like parameter aliases:

 [parameter(Mandatory=$true, Position=0)][Alias("name","path")][String]$filename 

But for functions instead.

+6
source share
3 answers

It seems that there is no solution that I am looking for.

So I had to use Export-ModuleMember

 Export-ModuleMember -Function * -Alias * 

At first, I just used the โ€œAliasโ€ parameter, because functions were exported correctly thanks to the manifest (FunctionsToExport = "*"), but then only aliases were exported.

Therefore, make sure that you export everything you want to export using the Export-ModuleMember cmdlet.

+5
source

Adding the -Scope Global command to the New-Alias โ€‹โ€‹command seems to do the trick.

 New-Alias -Name test -Value oldFunctionName -Scope Global 

While I was trying to do this, I noticed something that surprised me. I have a function in a module whose purpose is to create aliases. I was surprised to see that when I use this function (after the module has been imported), the aliases that it creates are associated with the module. If I remove the module, all the aliases that I created using this function will also disappear.

+3
source

If you look at:

 get-help New-ModuleManifest -full 

For -AliasesToExport you can see the following:

 -AliasesToExport <string[]> Specifies the aliases that the module exports. Wildcards are permitted. You can use this parameter to restrict the aliases that are exported by the module. It can remove aliases from the list of exported aliases, but it cannot add aliases to the list. If you omit this parameter, New-ModuleManifest creates an AliasesToExport key with a value of * (all), meaning that all aliases that are exported by the module are exported by the manifest. 

Maybe I'm wrong, but in my understanding -AliasesToExport can be used to limit the exported alias, but the sentence "New-ModuleManifest creates an AliasesToExport key with the value * (all), which means that all aliases exported by the module are exported by the manifest" means that You must export the alias to your module.

+2
source

Source: https://habr.com/ru/post/885986/


All Articles