【问题标题】:PowerShell two parameters either of them can be null but not both nullPowerShell 两个参数中的任何一个都可以为空,但不能同时为空
【发布时间】:2017-06-07 15:29:10
【问题描述】:

我想知道这是否可以在参数级别而不是在代码中完成: 我有 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
)
}

但是这样不行……

希望我正确地解释了这一点。或者如果没有办法仅在代码内部从参数执行此操作?

谢谢 河流

【问题讨论】:

    标签: validation powershell parameters null


    【解决方案1】:

    更新。以下注释代码 sn-p 基于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
    

    原始答案(代码 sn-p 基于 PowerShell 团队博客中的 PowerShell V2: ParameterSets 文章MSDN help article不允许 同时提供 both -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 ""
    

    【讨论】:

    • 非常感谢!我认为这是正确的答案!这意味着变量应该只出现一次,但参数条件不同!
    【解决方案2】:

    您可以在代码中测试它们,而不是尝试将此检查填充到参数集中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 2016-04-19
      • 2019-05-04
      • 2017-01-17
      • 1970-01-01
      相关资源
      最近更新 更多