PowerShell two parameters either of them can be nu

2019-08-26 23:11发布

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

2条回答
老娘就宠你
2楼-- · 2019-08-26 23:29

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!"}
查看更多
等我变得足够好
3楼-- · 2019-08-26 23:34

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 ""
查看更多
登录 后发表回答