簡體   English   中英

如何使用模塊清單導出 PowerShell 模塊別名?

[英]How to export PowerShell module aliases with a module manifest?

我有一個具有多種功能的模塊。

由於我以非 PowerShell 的方式命名它們,因此我想重命名它們。 但是由於模塊已經在使用中,我想保留舊的函數名稱。

實現這一目標的最佳方法似乎是使用別名。 我已經有一個模塊清單,其中指出:

AliasesToExport = '*'

所以我用New-Alias -Name test -Value oldFunctionName在模塊中創建了一個別名。

函數照常導入,但別名不存在。

我知道我可以在模塊中使用 Export-ModuleMember。 但是我有一個清單,它已經應該解決這個問題。

所以最后是我的問題:

為什么不通過清單導出別名?

函數本身是否有一個特殊的地方,我可以或必須在其中定義別名? 或者我是否必須在特殊的地方使用 New-Alias cmdlet?

我在想像參數別名這樣的東西:

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

但是對於函數。

似乎沒有我正在尋找的解決方案。

所以我不得不使用 Export-ModuleMember

Export-ModuleMember -Function * -Alias *

起初我只使用參數“別名”,因為函數應該由清單導出(FunctionsToExport =“*”),但實際上只導出了別名。

因此,請確保使用 Export-ModuleMember cmdlet 導出要導出的所有內容。

將 -Scope Global 添加到 New-Alias 命令似乎可以解決問題。

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

當我嘗試這個時,我注意到了一些讓我感到驚訝的事情。 我在一個模塊中有一個函數,其目的是創建別名。 我很驚訝地看到當我使用這個函數時(在導入模塊之后)它創建的別名與模塊相關聯。 如果我刪除模塊,我用這個函數創建的所有別名也會消失。

如果你看:

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 鍵,這意味着模塊導出的所有別名由清單導出”對我來說意味着您必須在模塊中導出別名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM