如何将版本和源信息添加到 powershell 别名?
How do I add Version and Source info to a powershell alias?
我在我的 powershell 配置文件(start-up 脚本)中设置了多个别名。
这样做 alias
(或等效的 Get-Alias
),将向您显示一个包含 headers 的列表,显示: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 to add version info to my powershell script?
- How to alias a Powershell function?
How do I add the Version and Source info to my own powershell alias?
您不能直接这样做,因为此信息来自别名'封闭模块。
换句话说:别名本身不包含此信息,只包含它的模块的一部分,如果有.
相反,这意味着在模块 外部 定义的别名 - 例如交互式定义的别名或通过 $PROFILE
定义的别名 - not 包含此信息。
另一件需要注意的事情:Get-Alias
只识别 已经导入 到会话中的模块的别名 - module auto-loading 是 not 在这种情况下触发。
这意味着来自 auto-loading 模块的别名只有 Get-Alias
知道,如果该模块已经通过其他方式加载(导入),特别是通过 调用 模块的任何命令。
下面的 示例代码 创建了一个(临时)模块 Foo
,带有模块清单 (*.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
我在我的 powershell 配置文件(start-up 脚本)中设置了多个别名。
这样做 alias
(或等效的 Get-Alias
),将向您显示一个包含 headers 的列表,显示: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 to add version info to my powershell script?
- How to alias a Powershell function?
How do I add the Version and Source info to my own powershell alias?
您不能直接这样做,因为此信息来自别名'封闭模块。
换句话说:别名本身不包含此信息,只包含它的模块的一部分,如果有.
相反,这意味着在模块 外部 定义的别名 - 例如交互式定义的别名或通过 $PROFILE
定义的别名 - not 包含此信息。
另一件需要注意的事情:Get-Alias
只识别 已经导入 到会话中的模块的别名 - module auto-loading 是 not 在这种情况下触发。
这意味着来自 auto-loading 模块的别名只有 Get-Alias
知道,如果该模块已经通过其他方式加载(导入),特别是通过 调用 模块的任何命令。
下面的 示例代码 创建了一个(临时)模块 Foo
,带有模块清单 (*.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