如何将版本和源信息添加到powershell别名



我在powershell配置文件(启动脚本(中设置了许多别名。

因此,执行alias(或等效的Get-Alias(将向您显示一个包含标题的列表,其中显示:命令类型、名称、版本。然而,对于除少数字段外的所有字段,VersionSource字段都是空的,还有其他几个可用字段,但仅在您这样做时显示:

# 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别名中


可能相关的问题:

  • 如何将版本信息添加到我的powershell脚本中
  • 如何别名Powershell函数

如何将版本和源信息添加到我自己的powershell别名中?

您不能直接执行,因为此信息来自别名'封闭模块

换句话说:别名本身不包含此信息,仅包含它所属的模块如果有

相反,这意味着在模块的外部定义的别名(如交互式定义的别名或通过$PROFILE定义的别名(不包含此信息。

另一件需要注意的事情是:Get-Alias只识别已经导入会话的模块的别名-在这种情况下,模块自动加载不是触发的。这意味着,只有当自动加载模块的别名已经通过其他方式加载(导入(,特别是通过调用模块的任何命令时,Get-Alias才知道该模块的别名。


下面的示例代码创建了一个(临时(模块Foo,带有模块清单(*.psd1(以启用版本控制,并添加了两个别名foo1foo2。然后导入该模块,并使用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

最新更新