locked
Multiple switch-Parameters do not work RRS feed

  • Question

  • [CmdletBinding(DefaultParameterSetName='All')]
    param(
            [Parameter(
                Mandatory=$true
            )]
            [ValidateLength(5,63)]
            [ValidatePattern("[a-z][.]{1}[a-z]")]
            [string]$DomainName,
            [Parameter(
                ParameterSetName='SecondDC',
                Mandatory=$false
            )]
            [switch]$SecondDC,
            [Parameter(
                ParameterSetName='SecondDC',
                Mandatory=$true
            )]
            [ipaddress]$SecondDCIP,
        
            [Parameter(
                ParameterSetName='Fileserver', 
                Mandatory=$false
            )]
            [switch]$Fileserver,
            [Parameter(
                ParameterSetName='Fileserver', 
                Mandatory=$true
            )]
            [ipaddress]$FileserverIP
        )

    Hi together,

    I have a strange problem and unfortunately find nothing to it:

    With my parameter-list above I can just use one of my switch parameters.

    For example:

    test.ps1 -DomainName "test.local" -SecondDC -SecondDCIP 10.0.0.2 #is possible
    
    test.ps1 -DomainName "test.local" -Fileserver -FileserverIP 10.0.0.1 #is possible
    
    test.ps1 -DomainName "test.local" -Fileserver -FileserverIP 10.0.0.1 -SecondDC -SecondDCIP 10.0.0.2 #not possible, Error: "The parameter set cannot be resolved with the specified named parameters." 
    
    

    Where's my fault, I don't understand...

    Thank you for every answer and every help!

    Saturday, August 11, 2018 12:33 PM

All replies

  • yours parameters configured in different parameters set.

    so you have 2 options:

    1 remove all parametersetname from your param block

    2 change or add parametersetname which would allow use all needed parameters in one command


    The opinion expressed by me is not an official position of Microsoft

    Saturday, August 11, 2018 12:47 PM
  • Okay, thanks for answer.

    But I want all my examples to work.
    So: If there is a file server, then I want to query some specific parameters, if there is a second DC, then I want to query some specific parameters and if there are both, then I want to query all parameters.
    Saturday, August 11, 2018 1:11 PM
  • in your 3rd example which does not work you need have some parameter set with the same name in all cases, it is possible but will not have a big sense:

    [CmdletBinding(DefaultParameterSetName='SecondDC')] param( [Parameter( Mandatory=$true )] [ValidateLength(5,63)] [ValidatePattern("[a-z][.]{1}[a-z]")] [string]$DomainName, [Parameter( ParameterSetName='SecondDC', Mandatory=$false )]

    [Parameter(

    ParametersetName='AllParams',

    Mandatory=$False

    )]

    [switch]$SecondDC, [Parameter( ParameterSetName='SecondDC', Mandatory=$true )]

    [Parameter(

    ParametersetName='AllParams',

    Mandatory=$False

    )]

    [ipaddress]$SecondDCIP, [Parameter( ParameterSetName='Fileserver', Mandatory=$false )]

    [Parameter(

    ParametersetName='AllParams',

    Mandatory=$False

    )]

    [switch]$Fileserver,

    [Parameter(ParameterSetName='Fileserver',Mandatory=$true )]

    [Parameter(

    ParametersetName='AllParams',

    Mandatory=$True

    )]

    [ipaddress]$FileserverIP )


    The opinion expressed by me is not an official position of Microsoft

    • Proposed as answer by Vector BCO Friday, October 19, 2018 6:13 AM
    Saturday, August 11, 2018 1:55 PM