简体   繁体   中英

How to export PowerShell module aliases with a module manifest?

I have a module with multiple functions.

Since I've named them in a not-PowerShell-way I want to rename them. But as the module is already in use I want to keep the old function names.

The best way to achieve this, seems to use aliases. I already have a module manifest which states:

AliasesToExport = '*'

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

The functions were imported as usual, but the alias was not there.

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

So here are finally my questions:

Why are the aliases not exported through the manifest?

Is there a special place in the function itself, where I can or must define an alias? Or do I have to use the New-Alias cmdlet somewhere special?

I was thinking of something like the parameter aliases:

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

But for functions instead.

There does not seem to be the solution I am looking for.

So I had to use Export-ModuleMember

Export-ModuleMember -Function * -Alias *

At first I just used the parameter "Alias" as the Functions were supposed to be exported by the manifest (FunctionsToExport = "*"), but then just the aliases were actually exported.

So make sure that you export everything you want to be exported with the Export-ModuleMember cmdlet.

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

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

While I was trying 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 it creates are associated with the module. If I remove the module, all of the aliases I created with this function go away too.

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.

I may be wrong, but in my understanding -AliasesToExport may be used to restrict an exported alias, but the sentence "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" means for me that you have to export the alias in your module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM