Validation PowerShell两个参数其中一个可以为null,但不能同时为null
我想知道这是否可以在参数级别而不是在代码中完成: 我有两个参数:$School和$ClassValidation PowerShell两个参数其中一个可以为null,但不能同时为null,validation,powershell,parameters,null,Validation,Powershell,Parameters,Null,我想知道这是否可以在参数级别而不是在代码中完成: 我有两个参数:$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
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
)
}
但它不是这样工作的
希望我解释得对。或者,若并没有任何方法可以仅从代码内部的参数执行此操作
谢谢
River您可以在代码中测试它们,而无需尝试将此检查填充到参数集中
if ($School -eq $null -and $Class -eq $null) {throw "School and Class must not be null together!"}
已更新。下面的注释代码段基于本文。允许同时提供
-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团队博客上文章的代码片段)不允许同时提供-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 ""
非常感谢!我认为这是正确的答案!这意味着变量只能出现一次,但参数条件不同!