PowerShell两个参数中的任何一个都可以为null,但不能同时为null [英] PowerShell two parameters either of them can be null but not both null

查看:251
本文介绍了PowerShell两个参数中的任何一个都可以为null,但不能同时为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在参数级别而不是代码中完成: 我有2个参数:$ School和$ Class

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. 

我尝试使用以下内容:

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?

谢谢 河

推荐答案

已更新.以下注释代码段基于

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

原始答案(基于 PowerShell V2:ParameterSets 文章,来自 PowerShell Team Blog

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

这篇关于PowerShell两个参数中的任何一个都可以为null,但不能同时为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆