如何使用模块清单导出PowerShell模块别名?

时间:2011-04-15 13:01:25

标签: powershell module manifest powershell-v2.0

我有一个具有多种功能的模块。

由于我以非PowerShell的方式命名它们,我想重命名它们。但是由于模块已经在使用中,我想保留旧的函数名称。

实现这一目标的最佳方法似乎是使用别名。我已经有一个模块清单说明:

AliasesToExport = '*'

所以我在模块中使用New-Alias -Name test -Value oldFunctionName创建了一个别名。

这些功能像往常一样导入,但别名不存在。

我知道我可以在模块中使用Export-ModuleMember。但我有一个已经应该照顾的清单。

所以最后我的问题是:

为什么不通过清单导出别名?

函数本身是否有特殊位置,我可以或者必须定义别名?或者我是否必须在特殊地方使用New-Alias cmdlet?

我在考虑类似参数别名的内容:

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

但是对于功能而言。

3 个答案:

答案 0 :(得分:6)

似乎没有我想要的解决方案。

所以我不得不使用Export-ModuleMember

Export-ModuleMember -Function * -Alias *

首先我使用了参数“Alias”,因为函数被正确导出,这要归功于清单(FunctionsToExport =“*”),但只是导出了别名。

因此,请确保使用Export-ModuleMember cmdlet导出要导出的所有内容。

答案 1 :(得分:3)

将-Scope Global添加到New-Alias命令似乎可以解决问题。

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

当我尝试这个时,我注意到一些让我感到惊讶的事情。我在一个模块中有一个函数,其目的是创建别名。我很惊讶地看到,当我使用此功能时(在导入模块之后),它创建的别名与模块相关联。如果我删除该模块,我使用此功能创建的所有别名也会消失。

答案 2 :(得分:2)

如果你看一下:

get-help New-ModuleManifest -full

对于-AliasesToExport,您可以看到以下内容:

-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.

我可能错了,但在我的理解中-AliasesToExport可用于限制导出的别名,但句子“New-ModuleManifest创建一个值为*(all)的AliasesToExport键,意思是清单导出模块导出的所有别名“对我来说意味着您必须在模块中导出别名。

相关问题