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
You can test them in code instead of trying to stuff this check into parameter set.
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.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: