PowerShell two parameters either of them can be null but not both null

眉间皱痕 提交于 2019-12-25 16:42:22

问题


I'm wondering if this can be done at the parameter level rather in code: I have 2 parameter: $School and $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. 

I tried to use following:

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
)
}

But it doesn't work in this way...

Hope I explained this correctly. Or if there is no way can do this from parameter only inside of the code?

Thanks River


回答1:


Updated. The following commented code snippet is based on Add a parameter to multiple Parameter Sets article. Allows supplying -Class or -School parameter or both of them all together.

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

Original answer (a code snippet based on PowerShell V2: ParameterSets article from PowerShell Team Blog and on MSDN help article) does not allow supplying both -Class and -School parameters all together:

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 ""



回答2:


You can test them in code instead of trying to stuff this check into parameter set.

if ($School -eq $null -and $Class -eq $null) {throw "School and Class must not be null together!"}


来源:https://stackoverflow.com/questions/44383629/powershell-two-parameters-either-of-them-can-be-null-but-not-both-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!