我试图在powershell中编写一个函数,它将使用一个或两个参数。
一个参数集将采用名字和姓氏 另一个会使用用户名。 显然,它们都是字符串,但我希望用户能够只键入
Get-Info <Username>
或
Get-Info <FirstName> <LastName>
无需命名参数。 但是,如果他们DID命名参数,他们可能只放置一个FirstName或只有一个LastName,这将是好的。 e.g。
Get-Info -FirstName John
Get-Info -LastName Doe
有关如何在命令中没有命名参数的情况下将Powershell默认为正确大小写的任何提示?
包含错误的代码示例:
clear-host
function Get-Info {
[CmdletBinding(DefaultParameterSetName='Username',PositionalBinding=$true)]
param (
[Parameter(ParameterSetName='Username', Position=0)]
[string]$Username
,
[Parameter(ParameterSetName='Name', Position=0)]
[string]$FirstName
,
[Parameter(ParameterSetName='Name', Position=1)]
[string]$LastName
)
process {
"Username: $Username"
"FirstName: $FirstName"
"LastName: $LastName"
}
}
Get-Info "myUsername"
Get-Info "myFirst" "myLast" #this doesn't work, as myFirst is bound to Username, and the Username parameterset has no second parameter
Get-Info -Username "Username by Param Name"
Get-Info -FirstName "FirstOnly"
Get-Info -LastName "LastOnly"
答案 0 :(得分:2)
解决方法是使用ValueFromPipeline
以不同的方式传递仅用户名参数; e.g。
clear-host
function Get-Info {
[CmdletBinding(DefaultParameterSetName='Username',PositionalBinding=$true)]
param (
[Parameter(ParameterSetName='Username', ValueFromPipeline=$true)]
[string]$Username
,
[Parameter(ParameterSetName='Name', Position=0)]
[string]$FirstName
,
[Parameter(ParameterSetName='Name', Position=1)]
[string]$LastName
)
process {
"Username: $Username"
"FirstName: $FirstName"
"LastName: $LastName"
}
}
"myUsername" | Get-Info
Get-Info "myFirst" "myLast" | write-host -ForegroundColor Cyan
Get-Info -Username "Username by Param Name"
Get-Info -FirstName "FirstOnly" | write-host -ForegroundColor Cyan
Get-Info -LastName "LastOnly"
虽然不是一个好的解决方案;特别是因为有更好的解决方法(即使用命名参数)。
我无法找到实现超载的方法,正如您所描述的那样;虽然我明白为什么你期望它成为可能。
<强>更新强>
另一种解决方法是提供虚拟参数,然后添加逻辑以重新映射参数的值; e.g。
clear-host
function Get-Info {
[CmdletBinding(DefaultParameterSetName='Username',PositionalBinding=$true)]
param (
[Parameter(ParameterSetName='Username', Position=0)]
[string]$Username
,
[Parameter(ParameterSetName='Username', Position=1)]
[string]$Hack = $null
,
[Parameter(ParameterSetName='Name', Position=0)]
[string]$FirstName
,
[Parameter(ParameterSetName='Name', Position=1)]
[string]$LastName
)
begin {
if (($PSCmdlet.ParameterSetName -eq 'Username') -and (-not [String]::IsNullOrEmpty($Hack) )) {
$PSCmdlet_ParameterSetName = 'Name' #since we can't assign to $PSCmdlet.ParameterSetName we may want a local variable to say which parameter set we're using
$FirstName = $Username
$LastName = $Hack
$Username = $null
$Hack = $null
} else {
$PSCmdlet_ParameterSetName = $PSCmdlet.ParameterSetName
}
}
process {
"Username: $Username"
"FirstName: $FirstName"
"LastName: $LastName"
}
}
Get-Info "myUsername"
Get-Info "myFirst" "myLast" | write-host -ForegroundColor Cyan
Get-Info -Username "Username by Param Name"
Get-Info -FirstName "FirstOnly" | write-host -ForegroundColor Cyan
Get-Info -LastName "LastOnly"
答案 1 :(得分:0)
您可以在参数声明中使用position属性来设置参数的顺序,这意味着您不必为参数本身命名,只需要指定值。
&#34;未指定Position关键字时,必须使用其名称引用cmdlet参数。&#34;
https://technet.microsoft.com/en-us/library/ms714348(v=vs.85).aspx