我想知道这是否可以在参数级别而不是代码中完成: 我有2个参数:$ School和$ Class
Get-Students -School SchooName # Will give me all Students name in all classes in that School
Get-Students -Class ClassName # Will give me all Students name in that Class across all schools
Get-Students # Should return parameter level error.
我尝试使用以下内容:
function Get-Students{
param(
[parameter(ParameterSetName="School no null")]
[ValidateNotNullOrEmpty()]
[string]$School,
[parameter(ParameterSetName="School no null")]
[AllowNull()]
[string]$Class,
[parameter(ParameterSetName="Class no null")]
[AllowNull()]
[string]$School,
[parameter(ParameterSetName="Class no null")]
[ValidateNotNullOrEmpty()]
[string]$Class
)
}
但它并没有以这种方式运作......
希望我正确解释了这一点。或者,如果没有办法只能在代码内部的参数中执行此操作?
由于 河
答案 0 :(得分:0)
您可以在代码中测试它们,而不是尝试将此检查填入参数集。
if ($School -eq $null -and $Class -eq $null) {throw "School and Class must not be null together!"}
答案 1 :(得分:0)
<强>更新即可。以下评论代码段基于Add a parameter to multiple Parameter Sets文章。允许同时提供-Class
或-School
参数或两者。
function Get-Students{
param(
[parameter(Mandatory=$false)][switch]$SomeThing, # any type instead of [switch]
# ↑↑↑ parameter(s) in all Parameter Sets
[parameter(Mandatory=$true, ParameterSetName="School No Null")]
[parameter(Mandatory=$true, ParameterSetName="Class And School")]
[ValidateNotNullOrEmpty()]
[string]$School,
[parameter(Mandatory=$true, ParameterSetName="Class No Null")]
[parameter(Mandatory=$true, ParameterSetName="Class And School")]
[ValidateNotNullOrEmpty()]
[string]$Class
)
Write-Host $PsCmdlet.ParameterSetName -ForegroundColor Cyan
switch ($PsCmdlet.ParameterSetName)
{
"School No Null" { "$School/*,$($SomeThing.IsPresent)"; break}
"Class No Null" { "*/$Class,$($SomeThing.IsPresent)"; break}
"Class And School" { "$School/$Class,$($SomeThing.IsPresent)"; break}
}
}
Get-Students -Class "A"
Get-Students -School "West" -SomeThing
Get-Students -Class "A" -School "West"
### errors raised:
# Get-Students
### Parameter set cannot be resolved using the specified named parameters.
### + FullyQualifiedErrorId : AmbiguousParameterSet,Get-Students
# Get-Students -Class ""
# Get-Students -School ""
### Cannot validate argument on parameter '…'. The argument is null or empty.
### + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-Students
原始回答(基于来自 PowerShell团队博客的PowerShell V2: ParameterSets文章和MSDN help article上的的代码段不允许< / strong>同时提供 -Class
和-School
参数:
function Get-Students{
param(
[parameter(Mandatory=$true, ParameterSetName="School No Null")][ValidateNotNullOrEmpty()]
[parameter(Mandatory=$false, ParameterSetName="Class No Null")] #[AllowNull()]
[string]$School,
[parameter(Mandatory=$true, ParameterSetName="Class No Null")] [ValidateNotNullOrEmpty()]
[parameter(Mandatory=$false, ParameterSetName="School No Null")]#[AllowNull()]
[string]$Class
)
Write-Host $PsCmdlet.ParameterSetName -ForegroundColor Cyan
switch ($PsCmdlet.ParameterSetName)
{
"School No Null" { $School; break}
"Class No Null" { $Class; break}
}
}
### valid calls:
Get-Students -Class "A"
Get-Students -School "West"
### errors raised:
### Parameter set cannot be resolved using the specified named parameters.
# Get-Students
# Get-Students -Class "A" -School "West"
### The argument is null or empty.
# Get-Students -Class ""
# Get-Students -School ""