如何使用模块清单导出 PowerShell 模块别名?
我有一个具有多种功能的模块。
由于我以非 PowerShell 方式命名它们,所以我想重命名它们。但由于该模块已经在使用中,我想保留旧的函数名称。
实现此目的的最佳方法似乎是使用别名。我已经有一个模块清单,其中指出:
AliasesToExport = '*'
因此我使用 New-Alias -Name test -Value oldFunctionName
在模块中创建了一个别名。
函数像往常一样导入,但别名不存在。
我知道我可以在模块中使用 Export-ModuleMember 。但我有一个清单已经应该解决这个问题了。
所以最后我的问题是:
为什么别名不通过清单导出?
函数本身是否有一个特殊的地方,我可以或必须在其中定义别名?或者我是否必须在特殊的地方使用 New-Alias cmdlet?
我正在考虑类似参数别名的东西:
[parameter(Mandatory=$true, Position=0)][Alias("name","path")][String]$filename
但是对于函数来说。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎没有我正在寻找的解决方案。
所以我必须使用 Export-ModuleMember
起初我只使用参数“Alias”,因为函数应该由清单导出(FunctionsToExport =“*”),但实际上只导出了别名。
因此,请确保使用 Export-ModuleMember cmdlet 导出要导出的所有内容。
There does not seem to be the solution I am looking for.
So I had to use Export-ModuleMember
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.
将 -Scope Global 添加到 New-Alias 命令似乎可以解决问题。
当我尝试这样做时,我注意到一些令我惊讶的事情。我的模块中有一个函数,其目的是创建别名。我惊讶地发现,当我使用此函数时(导入模块后),它创建的别名与该模块关联。如果我删除该模块,我用此函数创建的所有别名也会消失。
Adding -Scope Global to the New-Alias command seems to do the trick.
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.
如果您查看:
对于
-AliasesToExport
您可以看到以下内容:我可能是错的,但根据我的理解
-AliasesToExport
可能用于限制导出的别名,但句子“New-ModuleManifest 创建一个值为 *(全部)的 AliasesToExport 键,这意味着模块导出的所有别名都由清单导出” 对我来说意味着您必须导出模块中的别名。If you look at:
For
-AliasesToExport
you can see the following: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.