2017-06-06 43 views
0

我不知道這是否可以在參數級別來完成,而在代碼: 我有2個參數:$學校和$類PowerShell的兩個參數其中任何可以爲空但不能同時爲空

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

但它不以這種方式工作...

希望我解釋這個正確。或者如果沒有辦法可以從代碼內部的參數做到這一點?

感謝 河

回答

0

更新。以下已評論代碼片段是基於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 

原來的答覆(基於PowerShell V2: ParameterSets文章的代碼片段從PowerShell團隊博客MSDN help article不允許提供-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 "" 
+0

非常感謝!我認爲這是正確的答案!這意味着該變量應該只出現一次,但具有不同的參數條件! –

+0

@RiverYang確切。答案已更新。 – JosefZ

0

您可以測試他們的代碼,而不是試圖把這些東西支票存入參數集。

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