Advanced function with nested Parameter sets

This topic contains 2 replies, has 2 voices, and was last updated by  Jack van Deur 3 weeks, 2 days ago.

  • Author
    Posts
  • #80209

    Jack van Deur
    Participant

    Hi,

    I am working on this function where I need to use parameter sets because I have 2 parameters that cannot be used together.
    Var1 in set 1 and var2 in set2.
    That works fine.

    I also have var 3 which is a switch and when that switch is set var4 is mandatory.
    Therefore I put var3 and var4 in their own ParameterSet set3

    Furthermore there is a parameter var5.
    When var5 is set var6 is mandatory
    Therefore I put them in Set4

    Var1 should be mandatory for all sets except Set2
    Var2 should be mandatory for all sets except Set1

    This is what I think should be the param block:

    function Get-Something 
    {
        [CmdletBinding(DefaultParameterSetName='Set1')]
        Param
        (
            [Parameter(ParameterSetName='Set1',
                       Position = 0,
                       Mandatory=$True)]
                       [String]
                       $Var1,
    
            [Parameter(ParameterSetName='Set2',
                       Position = 0,
                       Mandatory=$True)]
                       [String]
                       $Var2,
    
            [Parameter(ParameterSetName='Set1', Mandatory = $False)]
            [Parameter(ParameterSetName='Set2', Mandatory = $False)]
            [Parameter(ParameterSetName='Set3',
                       Position = 1,
                       Mandatory=$False)]
                       [switch]
                       $Var3,
                       
    
            [Parameter(ParameterSetName='Set1', Mandatory = $False)]
            [Parameter(ParameterSetName='Set2', Mandatory = $False)]
            [Parameter(ParameterSetName='Set3',
                       Position = 2,
                       Mandatory=$True)]
                       [Array]
                       $Var4,
    
            [Parameter(ParameterSetName='Set1', Mandatory = $False)]
            [Parameter(ParameterSetName='Set2', Mandatory = $False)]
            [Parameter(ParameterSetName='Set4',
                       Position = 3,
                       Mandatory=$False)]
                       $Var5,
    
    
            [Parameter(ParameterSetName='Set1', Mandatory = $False)]
            [Parameter(ParameterSetName='Set2', Mandatory = $False)]
            [Parameter(ParameterSetName='Set4',
                       Position = 4,
                       Mandatory=$True)]
    		   [String]
                       $Var6
    
         )
    }
    

    This is what I get when I run this function:

    SYNTAX
        Get-Something [-Var1]  [-Var3] [-Var4 ] [-Var5 ] [-Var6 ]  []
        
        Get-Something [-Var2]  [-Var3] [-Var4 ] [-Var5 ] [-Var6 ]  []
        
        Get-Something [[-Var3]] [-Var4]   []
        
        Get-Something [[-Var5] ] [-Var6]   []
    

    Line 1 looks fine but when I set var3, the function never asks for var4 same counts for var5 and var6
    Same counts for line 2
    Line 3 should show var4 as mandatory which it is not
    Line 4 should show var6 as mandatory which it is not.
    Last, line 3 and 4 imply that it should be possible to run the command without var1 or var2 which can never be the case.

    Can someone enlighten me?

    Thank you in advance.

  • #80281

    Christian Sandfeld
    Participant

    If I understand your scenario correctly, I think this should achive what you want.

    function Get-Something 
    {
        [CmdletBinding(DefaultParameterSetName='Set1')]
        Param(
            [Parameter(ParameterSetName='Set1', Mandatory = $True)]
            [Parameter(ParameterSetName='Set1a', Mandatory = $True)]
            [Parameter(ParameterSetName='Set1b', Mandatory = $True)]
            [Parameter(ParameterSetName='Set1ab', Mandatory = $True)]
            [String]
            $Var1,
    
            [Parameter(ParameterSetName='Set2', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2a', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2b', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2ab', Mandatory = $True)]
            [String]
            $Var2,
    
            [Parameter(ParameterSetName='Set1a', Mandatory = $True)]
            [Parameter(ParameterSetName='Set1ab', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2a', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2ab', Mandatory = $True)]
            [switch]
            $Var3,
    
            [Parameter(ParameterSetName='Set1a', Mandatory = $True)]
            [Parameter(ParameterSetName='Set1ab', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2a', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2ab', Mandatory = $True)]
            [Array]
            $Var4,
    
            [Parameter(ParameterSetName='Set1b', Mandatory = $True)]
            [Parameter(ParameterSetName='Set1ab', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2b', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2ab', Mandatory = $True)]
            $Var5,
    
    
            [Parameter(ParameterSetName='Set1b', Mandatory = $True)]
            [Parameter(ParameterSetName='Set1ab', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2b', Mandatory = $True)]
            [Parameter(ParameterSetName='Set2ab', Mandatory = $True)]
            [String]
            $Var6
         )
    }

    And here are the resulting syntaxes

    SYNTAX
        Get-Something -Var1   []
        
        Get-Something -Var1  -Var3 -Var4  -Var5  -Var6   []
        
        Get-Something -Var1  -Var5  -Var6   []
        
        Get-Something -Var1  -Var3 -Var4   []
        
        Get-Something -Var2  -Var3 -Var4  -Var5  -Var6   []
        
        Get-Something -Var2  -Var5  -Var6   []
        
        Get-Something -Var2  -Var3 -Var4   []
        
        Get-Something -Var2   []

    (forum eats some of the syntax text)

    • #80405

      Jack van Deur
      Participant

      Thank you Christian,

      working although not asking for value if value is missing but erroring out it, works.

      Regards, Jack

You must be logged in to reply to this topic.