在高级 PowerShell 函数中声明多个别名
Declaring multiple aliases in an advanced PowerShell function
用于在 PowerShell cmdlet 中声明别名的 documentation 显示如下:
Function Get-SomeValue {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)]
[Alias("MachineName")]
[string[]]$ComputerName
)
Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName
}
我使用什么语法来创建多个别名?
[Alias("one","two","three)]
[Alias("one")][Alias("two")][Alias("three")]
以上- None
- 还有别的吗?
P.S。使用 Get-Help
时,应该在何处显示别名?到目前为止我还没有看到它们。
[Alias("one")][Alias("two")]
和 [Alias("one", "two")]
都有效。当您显示参数的帮助时,您会看到别名:
PS C:\> Get-Help Get-SomeValue -Parameter computername
-ComputerName <string[]>
Required? true
Position? 0
Accept pipeline input? true (ByValue)
Parameter set name (All)
Aliases one, two
Dynamic? false
要补充 :
如果文档不足,您可以自己检查基础属性 class,前提是您熟悉 C#:
[Alias(...)]
表示 System.Management.Automation
命名空间中的 class AliasAttribute
,即 System.Management.Automation.AutomationAttribute
.
如果您在 https://docs.microsoft.com/en-us/dotnet/api, you'll find https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.aliasattribute?view=powershellsdk-1.1.0 中查找 class,其构造函数具有以下签名:
public AliasAttribute (params string[] aliasNames);
这告诉您 多个 别名可以作为单独的参数传递给 [Alias(...)]
。
用于在 PowerShell cmdlet 中声明别名的 documentation 显示如下:
Function Get-SomeValue {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)]
[Alias("MachineName")]
[string[]]$ComputerName
)
Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName
}
我使用什么语法来创建多个别名?
[Alias("one","two","three)]
[Alias("one")][Alias("two")][Alias("three")]
以上- None
- 还有别的吗?
P.S。使用 Get-Help
时,应该在何处显示别名?到目前为止我还没有看到它们。
[Alias("one")][Alias("two")]
和 [Alias("one", "two")]
都有效。当您显示参数的帮助时,您会看到别名:
PS C:\> Get-Help Get-SomeValue -Parameter computername -ComputerName <string[]> Required? true Position? 0 Accept pipeline input? true (ByValue) Parameter set name (All) Aliases one, two Dynamic? false
要补充
如果文档不足,您可以自己检查基础属性 class,前提是您熟悉 C#:
[Alias(...)]
表示 System.Management.Automation
命名空间中的 class AliasAttribute
,即 System.Management.Automation.AutomationAttribute
.
如果您在 https://docs.microsoft.com/en-us/dotnet/api, you'll find https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.aliasattribute?view=powershellsdk-1.1.0 中查找 class,其构造函数具有以下签名:
public AliasAttribute (params string[] aliasNames);
这告诉您 多个 别名可以作为单独的参数传递给 [Alias(...)]
。