我在我的 powershell 配置文件(启动脚本)中设置了多个别名。
这样做alias
(或等效的Get-Alias
),将显示一个带有标题的列表,显示:CommandType、Name、Version和来源。但是,除了少数几个之外,所有这些字段的 Version
和 Source
字段都是空的,并且还有其他几个可用字段,但仅在您执行以下操作时显示:
# Get-Alias upmo |select *
HelpUri : https://go.microsoft.com/fwlink/?LinkID=398576
ResolvedCommandName : Update-Module
DisplayName : upmo -> Update-Module
ReferencedCommand : Update-Module
ResolvedCommand : Update-Module
Definition : Update-Module
Options : None
Description :
OutputType : {}
Name : upmo
CommandType : Alias
Source : PowerShellGet
Version : 1.6.7
Visibility : Public
ModuleName : PowerShellGet
Module : PowerShellGet
RemotingCapability : PowerShell
Parameters : {[Name, System.Management.Automation.ParameterMetadata],
[RequiredVersion, System.Management.Automation.ParameterMetadata],
[MaximumVersion, System.Management.Automation.ParameterMetadata],
[Credential, System.Management.Automation.ParameterMetadata]...}
ParameterSets :
Set-Alias
的 MS 文档没有说明如何访问或设置这些附加字段。
如何将版本和源信息添加到我自己的 powershell 别名中?
可能相关的问题:
最佳答案
How do I add the Version and Source info to my own powershell alias?
您不能直接执行此操作,因为此信息来自别名的封闭模块。
换句话说:别名本身不包含此信息,仅包含它所属的模块(如果有)。
相反,这意味着在模块外部定义的别名(例如交互式定义的别名或通过$PROFILE
定义的别名)不会包含此信息。
需要注意的另一件事:Get-Alias
仅识别已导入到 session 中的模块中的别名 - module auto-loading在这种情况下不会被触发。
这意味着,只有当该模块已通过其他方式加载(导入)时,尤其是通过调用任何模块的命令。
以下示例代码创建一个(临时)模块Foo
,并带有模块 list (*.psd1
)以启用版本控制,并添加两个别名:foo1
和 foo2
。然后导入该模块,并使用 Get-Command
获取有关别名的信息:
# Create module 'Foo':
# Create the module folder...
$tmpDir = [io.path]::GetTempPath() + '/' + $PID + '/Foo'
Push-Location (New-Item -Type Directory -Force $tmpDir)
# ... and a manifest with a version number and the aliases to export ...
New-ModuleManifest -Path ./Foo.psd1 -RootModule Foo.psm1 -ModuleVersion 1.0.1 -AliasesToExport foo1, foo2
# ... and the module implementation with the two aliases
@'
set-alias foo1 Get-Date
set-alias foo2 Get-Command
'@ > Foo.psm1
# Import the module. Add -Verbose to see import details.
Import-Module -Force ../Foo
# Get information about the aliases.
Get-Command -Type Alias foo1, foo2
# Clean up.
Pop-Location
Remove-Item -Recurse $tmpDir
上面的输出如下,其中显示了别名的来源模块以及后者的版本号:
CommandType Name Version Source
----------- ---- ------- ------
Alias foo1 -> Get-Date 1.0.1 Foo
Alias foo2 -> Get-Command 1.0.1 Foo
关于windows - 如何将版本和源信息添加到 powershell 别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64743623/