PowerShell Parameter in more than one ParameterSet returns ambigous error












0














I'm trying to write a PS function, that should give an activate or deactivate command depending on the switch parameter specified.



I want $Active and $Inactive in two different parameter set, but I want to specify $InputFile or $Policy with $Active$Inactive



function ModifyState (
[Parameter(Mandatory=$false,ParameterSetName="A")]
[Parameter(Mandatory=$false,ParameterSetName="I")]
[string]$InputFile,
[Parameter(Mandatory=$false,ParameterSetName="A")]
[Parameter(Mandatory=$false,ParameterSetName="I")]
[string]$Object,
[Parameter(Mandatory=$false,ParameterSetName="A")]
[switch]$Active,
[Parameter(Mandatory=$false,ParameterSetName="I")]
[switch]$Inactive

) {
switch ($PsCmdlet.ParameterSetName) {
"A" {
if ($InputFile) {
foreach ($obj in (gc $InputFile)) {
write-host "Activate $obj"
}
}
else {write-host "Activate $Object"}
}
"I" {
if ($InputFile) {
foreach ($obj in (gc $InputFile)) {
write-host "Deactivate $obj"
}
}
else {write-host "Deactivate $Object"}
}
}


When I run the command without specify parameters or when I specify "too many parameters" I get error about ambigous parameter set.



Command:



ModifyState -InputFile .temp.txt -Inactive -Active


Error:



ModifyState : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ ModifyState -InputFile .temp.txt -Inactive -Active
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [ModifyState], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,ModifyState


Question 1:
Is parameters code right?



Question 2:
Is there a way to return a default message when there's a parameter set ambiguity?



Hope I was clear and thank you in advance



db





Update 13/11/18
These are the examples of the commands that I want to give:



#Parameterset "I"
ModifyState -Object PARAMVALUE -Inactive
or
ModifyState -InputFile PARAMVALUE -Inactive

#Parameterset "A"
ModifyState -Object PARAMVALUE -Active
or
ModifyState -InputFile PARAMVALUE -Active


-Active and -Inactive must not given at the same time.
Also -InputFile and -Object must not given at the same time.



db










share|improve this question





























    0














    I'm trying to write a PS function, that should give an activate or deactivate command depending on the switch parameter specified.



    I want $Active and $Inactive in two different parameter set, but I want to specify $InputFile or $Policy with $Active$Inactive



    function ModifyState (
    [Parameter(Mandatory=$false,ParameterSetName="A")]
    [Parameter(Mandatory=$false,ParameterSetName="I")]
    [string]$InputFile,
    [Parameter(Mandatory=$false,ParameterSetName="A")]
    [Parameter(Mandatory=$false,ParameterSetName="I")]
    [string]$Object,
    [Parameter(Mandatory=$false,ParameterSetName="A")]
    [switch]$Active,
    [Parameter(Mandatory=$false,ParameterSetName="I")]
    [switch]$Inactive

    ) {
    switch ($PsCmdlet.ParameterSetName) {
    "A" {
    if ($InputFile) {
    foreach ($obj in (gc $InputFile)) {
    write-host "Activate $obj"
    }
    }
    else {write-host "Activate $Object"}
    }
    "I" {
    if ($InputFile) {
    foreach ($obj in (gc $InputFile)) {
    write-host "Deactivate $obj"
    }
    }
    else {write-host "Deactivate $Object"}
    }
    }


    When I run the command without specify parameters or when I specify "too many parameters" I get error about ambigous parameter set.



    Command:



    ModifyState -InputFile .temp.txt -Inactive -Active


    Error:



    ModifyState : Parameter set cannot be resolved using the specified named parameters.
    At line:1 char:1
    + ModifyState -InputFile .temp.txt -Inactive -Active
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [ModifyState], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,ModifyState


    Question 1:
    Is parameters code right?



    Question 2:
    Is there a way to return a default message when there's a parameter set ambiguity?



    Hope I was clear and thank you in advance



    db





    Update 13/11/18
    These are the examples of the commands that I want to give:



    #Parameterset "I"
    ModifyState -Object PARAMVALUE -Inactive
    or
    ModifyState -InputFile PARAMVALUE -Inactive

    #Parameterset "A"
    ModifyState -Object PARAMVALUE -Active
    or
    ModifyState -InputFile PARAMVALUE -Active


    -Active and -Inactive must not given at the same time.
    Also -InputFile and -Object must not given at the same time.



    db










    share|improve this question



























      0












      0








      0







      I'm trying to write a PS function, that should give an activate or deactivate command depending on the switch parameter specified.



      I want $Active and $Inactive in two different parameter set, but I want to specify $InputFile or $Policy with $Active$Inactive



      function ModifyState (
      [Parameter(Mandatory=$false,ParameterSetName="A")]
      [Parameter(Mandatory=$false,ParameterSetName="I")]
      [string]$InputFile,
      [Parameter(Mandatory=$false,ParameterSetName="A")]
      [Parameter(Mandatory=$false,ParameterSetName="I")]
      [string]$Object,
      [Parameter(Mandatory=$false,ParameterSetName="A")]
      [switch]$Active,
      [Parameter(Mandatory=$false,ParameterSetName="I")]
      [switch]$Inactive

      ) {
      switch ($PsCmdlet.ParameterSetName) {
      "A" {
      if ($InputFile) {
      foreach ($obj in (gc $InputFile)) {
      write-host "Activate $obj"
      }
      }
      else {write-host "Activate $Object"}
      }
      "I" {
      if ($InputFile) {
      foreach ($obj in (gc $InputFile)) {
      write-host "Deactivate $obj"
      }
      }
      else {write-host "Deactivate $Object"}
      }
      }


      When I run the command without specify parameters or when I specify "too many parameters" I get error about ambigous parameter set.



      Command:



      ModifyState -InputFile .temp.txt -Inactive -Active


      Error:



      ModifyState : Parameter set cannot be resolved using the specified named parameters.
      At line:1 char:1
      + ModifyState -InputFile .temp.txt -Inactive -Active
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [ModifyState], ParameterBindingException
      + FullyQualifiedErrorId : AmbiguousParameterSet,ModifyState


      Question 1:
      Is parameters code right?



      Question 2:
      Is there a way to return a default message when there's a parameter set ambiguity?



      Hope I was clear and thank you in advance



      db





      Update 13/11/18
      These are the examples of the commands that I want to give:



      #Parameterset "I"
      ModifyState -Object PARAMVALUE -Inactive
      or
      ModifyState -InputFile PARAMVALUE -Inactive

      #Parameterset "A"
      ModifyState -Object PARAMVALUE -Active
      or
      ModifyState -InputFile PARAMVALUE -Active


      -Active and -Inactive must not given at the same time.
      Also -InputFile and -Object must not given at the same time.



      db










      share|improve this question















      I'm trying to write a PS function, that should give an activate or deactivate command depending on the switch parameter specified.



      I want $Active and $Inactive in two different parameter set, but I want to specify $InputFile or $Policy with $Active$Inactive



      function ModifyState (
      [Parameter(Mandatory=$false,ParameterSetName="A")]
      [Parameter(Mandatory=$false,ParameterSetName="I")]
      [string]$InputFile,
      [Parameter(Mandatory=$false,ParameterSetName="A")]
      [Parameter(Mandatory=$false,ParameterSetName="I")]
      [string]$Object,
      [Parameter(Mandatory=$false,ParameterSetName="A")]
      [switch]$Active,
      [Parameter(Mandatory=$false,ParameterSetName="I")]
      [switch]$Inactive

      ) {
      switch ($PsCmdlet.ParameterSetName) {
      "A" {
      if ($InputFile) {
      foreach ($obj in (gc $InputFile)) {
      write-host "Activate $obj"
      }
      }
      else {write-host "Activate $Object"}
      }
      "I" {
      if ($InputFile) {
      foreach ($obj in (gc $InputFile)) {
      write-host "Deactivate $obj"
      }
      }
      else {write-host "Deactivate $Object"}
      }
      }


      When I run the command without specify parameters or when I specify "too many parameters" I get error about ambigous parameter set.



      Command:



      ModifyState -InputFile .temp.txt -Inactive -Active


      Error:



      ModifyState : Parameter set cannot be resolved using the specified named parameters.
      At line:1 char:1
      + ModifyState -InputFile .temp.txt -Inactive -Active
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [ModifyState], ParameterBindingException
      + FullyQualifiedErrorId : AmbiguousParameterSet,ModifyState


      Question 1:
      Is parameters code right?



      Question 2:
      Is there a way to return a default message when there's a parameter set ambiguity?



      Hope I was clear and thank you in advance



      db





      Update 13/11/18
      These are the examples of the commands that I want to give:



      #Parameterset "I"
      ModifyState -Object PARAMVALUE -Inactive
      or
      ModifyState -InputFile PARAMVALUE -Inactive

      #Parameterset "A"
      ModifyState -Object PARAMVALUE -Active
      or
      ModifyState -InputFile PARAMVALUE -Active


      -Active and -Inactive must not given at the same time.
      Also -InputFile and -Object must not given at the same time.



      db







      powershell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 16:24

























      asked Nov 12 '18 at 18:32









      karnak

      53




      53
























          2 Answers
          2






          active

          oldest

          votes


















          0














          With the updated requirements, you should be doing something like this:



          function ModifyState (
          [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
          [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
          [string]$InputFile,
          [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
          [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
          [string]$Object,
          [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
          [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
          [switch]$Active,
          [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
          [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
          [switch]$Inactive
          ) {
          switch ($PsCmdlet.ParameterSetName) {
          "A" {
          if ($InputFile) {
          foreach ($obj in (gc $InputFile)) {
          write-host "Activate $obj"
          }
          }
          else {write-host "Activate $Object"}
          }
          "I" {
          if ($InputFile) {
          foreach ($obj in (gc $InputFile)) {
          write-host "Deactivate $obj"
          }
          }
          else {write-host "Deactivate $Object"}
          }
          }
          }


          Explanation
          With the exclusion between the "object" parameters and the "active" parameters, you will need 4 different parametersets to fulfill your requirements.






          share|improve this answer





























            0














            With you example you shared with us, the following would be valid parametersets:



            #Parameterset "I"
            ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Inactive

            #Parameterset "A"
            ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Active


            If you only plan on having those 2 parametersets and you have some parameters that should be valid for both, you actually don't need to declare them in the parametersets - unless the should behave differently.



            So you example could be written like this:



            function ModifyState (    
            [string]$InputFile,
            [string]$Object,
            [Parameter(Mandatory = $false, ParameterSetName = "A")]
            [switch]$Active,
            [Parameter(Mandatory = $false, ParameterSetName = "I")]
            [switch]$Inactive) {
            switch ($PsCmdlet.ParameterSetName) {
            "A" {
            if ($InputFile) {
            foreach ($obj in (gc $InputFile)) {
            write-host "Activate $obj"
            }
            }
            else {write-host "Activate $Object"}
            }
            "I" {
            if ($InputFile) {
            foreach ($obj in (gc $InputFile)) {
            write-host "Deactivate $obj"
            }
            }
            else {write-host "Deactivate $Object"}
            }
            }
            }


            If you plan on executing your function without any parameters, you need to provide a [CmdletBinding(DefaultParameterSetName = 'Default')] section and provide default values for the parameters.



            function ModifyState {
            [CmdletBinding(DefaultParameterSetName = 'A')]
            param(
            [string]$InputFile = "Filepath",
            [string]$Object,
            [Parameter(Mandatory = $false, ParameterSetName = "A")]
            [switch]$Active = [switch]::Present,
            [Parameter(Mandatory = $false, ParameterSetName = "I")]
            [switch]$Inactive) {
            switch ($PsCmdlet.ParameterSetName) {
            "A" {
            if ($InputFile) {
            foreach ($obj in (gc $InputFile)) {
            write-host "Activate $obj"
            }
            }
            else {write-host "Activate $Object"}
            }
            "I" {
            if ($InputFile) {
            foreach ($obj in (gc $InputFile)) {
            write-host "Deactivate $obj"
            }
            }
            else {write-host "Deactivate $Object"}
            }
            }
            }
            }


            That would make it possible for you to run the function without parameters. The -Active will always be set and the -InputFile will have always point the to file you specify as the default value.



            With all that in place - what would you have expected from the ModifyState -InputFile .temp.txt -Inactive -Active ?



            Either you should be using:



            ModifyState -InputFile .temp.txt -Inactive



            or



            ModifyState -InputFile .temp.txt -Active



            with the current parametersets you have defined.



            Please update you question with more details about what you are trying to solve, so we can help you on your way.






            share|improve this answer





















            • Sorry, I was not clear at all. "ModifyState -InputFile .temp.txt -Inactive -Active" was a try to understand what the function would return, but it's not what I want, because -Active and -Inactive must not given at the same time. Also -InputFile and -Object must not given at the same time What I want is: #Parameterset "I" ModifyState -Object PARAMVALUE -Inactive or ModifyState -InputFile PARAMVALUE -Inactive #Parameterset "A" ModifyState -Object PARAMVALUE -Active or ModifyState -InputFile PARAMVALUE -Active
              – karnak
              Nov 13 '18 at 16:11











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268094%2fpowershell-parameter-in-more-than-one-parameterset-returns-ambigous-error%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            With the updated requirements, you should be doing something like this:



            function ModifyState (
            [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
            [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
            [string]$InputFile,
            [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
            [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
            [string]$Object,
            [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
            [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
            [switch]$Active,
            [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
            [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
            [switch]$Inactive
            ) {
            switch ($PsCmdlet.ParameterSetName) {
            "A" {
            if ($InputFile) {
            foreach ($obj in (gc $InputFile)) {
            write-host "Activate $obj"
            }
            }
            else {write-host "Activate $Object"}
            }
            "I" {
            if ($InputFile) {
            foreach ($obj in (gc $InputFile)) {
            write-host "Deactivate $obj"
            }
            }
            else {write-host "Deactivate $Object"}
            }
            }
            }


            Explanation
            With the exclusion between the "object" parameters and the "active" parameters, you will need 4 different parametersets to fulfill your requirements.






            share|improve this answer


























              0














              With the updated requirements, you should be doing something like this:



              function ModifyState (
              [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
              [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
              [string]$InputFile,
              [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
              [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
              [string]$Object,
              [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
              [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
              [switch]$Active,
              [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
              [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
              [switch]$Inactive
              ) {
              switch ($PsCmdlet.ParameterSetName) {
              "A" {
              if ($InputFile) {
              foreach ($obj in (gc $InputFile)) {
              write-host "Activate $obj"
              }
              }
              else {write-host "Activate $Object"}
              }
              "I" {
              if ($InputFile) {
              foreach ($obj in (gc $InputFile)) {
              write-host "Deactivate $obj"
              }
              }
              else {write-host "Deactivate $Object"}
              }
              }
              }


              Explanation
              With the exclusion between the "object" parameters and the "active" parameters, you will need 4 different parametersets to fulfill your requirements.






              share|improve this answer
























                0












                0








                0






                With the updated requirements, you should be doing something like this:



                function ModifyState (
                [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
                [string]$InputFile,
                [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
                [string]$Object,
                [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
                [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
                [switch]$Active,
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
                [switch]$Inactive
                ) {
                switch ($PsCmdlet.ParameterSetName) {
                "A" {
                if ($InputFile) {
                foreach ($obj in (gc $InputFile)) {
                write-host "Activate $obj"
                }
                }
                else {write-host "Activate $Object"}
                }
                "I" {
                if ($InputFile) {
                foreach ($obj in (gc $InputFile)) {
                write-host "Deactivate $obj"
                }
                }
                else {write-host "Deactivate $Object"}
                }
                }
                }


                Explanation
                With the exclusion between the "object" parameters and the "active" parameters, you will need 4 different parametersets to fulfill your requirements.






                share|improve this answer












                With the updated requirements, you should be doing something like this:



                function ModifyState (
                [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
                [string]$InputFile,
                [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
                [string]$Object,
                [Parameter(Mandatory = $false, ParameterSetName = "Active-File")]
                [Parameter(Mandatory = $false, ParameterSetName = "Active-Object")]
                [switch]$Active,
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-File")]
                [Parameter(Mandatory = $false, ParameterSetName = "Inactive-Object")]
                [switch]$Inactive
                ) {
                switch ($PsCmdlet.ParameterSetName) {
                "A" {
                if ($InputFile) {
                foreach ($obj in (gc $InputFile)) {
                write-host "Activate $obj"
                }
                }
                else {write-host "Activate $Object"}
                }
                "I" {
                if ($InputFile) {
                foreach ($obj in (gc $InputFile)) {
                write-host "Deactivate $obj"
                }
                }
                else {write-host "Deactivate $Object"}
                }
                }
                }


                Explanation
                With the exclusion between the "object" parameters and the "active" parameters, you will need 4 different parametersets to fulfill your requirements.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 16:42









                Mötz

                1,032711




                1,032711

























                    0














                    With you example you shared with us, the following would be valid parametersets:



                    #Parameterset "I"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Inactive

                    #Parameterset "A"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Active


                    If you only plan on having those 2 parametersets and you have some parameters that should be valid for both, you actually don't need to declare them in the parametersets - unless the should behave differently.



                    So you example could be written like this:



                    function ModifyState (    
                    [string]$InputFile,
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }


                    If you plan on executing your function without any parameters, you need to provide a [CmdletBinding(DefaultParameterSetName = 'Default')] section and provide default values for the parameters.



                    function ModifyState {
                    [CmdletBinding(DefaultParameterSetName = 'A')]
                    param(
                    [string]$InputFile = "Filepath",
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active = [switch]::Present,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }
                    }


                    That would make it possible for you to run the function without parameters. The -Active will always be set and the -InputFile will have always point the to file you specify as the default value.



                    With all that in place - what would you have expected from the ModifyState -InputFile .temp.txt -Inactive -Active ?



                    Either you should be using:



                    ModifyState -InputFile .temp.txt -Inactive



                    or



                    ModifyState -InputFile .temp.txt -Active



                    with the current parametersets you have defined.



                    Please update you question with more details about what you are trying to solve, so we can help you on your way.






                    share|improve this answer





















                    • Sorry, I was not clear at all. "ModifyState -InputFile .temp.txt -Inactive -Active" was a try to understand what the function would return, but it's not what I want, because -Active and -Inactive must not given at the same time. Also -InputFile and -Object must not given at the same time What I want is: #Parameterset "I" ModifyState -Object PARAMVALUE -Inactive or ModifyState -InputFile PARAMVALUE -Inactive #Parameterset "A" ModifyState -Object PARAMVALUE -Active or ModifyState -InputFile PARAMVALUE -Active
                      – karnak
                      Nov 13 '18 at 16:11
















                    0














                    With you example you shared with us, the following would be valid parametersets:



                    #Parameterset "I"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Inactive

                    #Parameterset "A"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Active


                    If you only plan on having those 2 parametersets and you have some parameters that should be valid for both, you actually don't need to declare them in the parametersets - unless the should behave differently.



                    So you example could be written like this:



                    function ModifyState (    
                    [string]$InputFile,
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }


                    If you plan on executing your function without any parameters, you need to provide a [CmdletBinding(DefaultParameterSetName = 'Default')] section and provide default values for the parameters.



                    function ModifyState {
                    [CmdletBinding(DefaultParameterSetName = 'A')]
                    param(
                    [string]$InputFile = "Filepath",
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active = [switch]::Present,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }
                    }


                    That would make it possible for you to run the function without parameters. The -Active will always be set and the -InputFile will have always point the to file you specify as the default value.



                    With all that in place - what would you have expected from the ModifyState -InputFile .temp.txt -Inactive -Active ?



                    Either you should be using:



                    ModifyState -InputFile .temp.txt -Inactive



                    or



                    ModifyState -InputFile .temp.txt -Active



                    with the current parametersets you have defined.



                    Please update you question with more details about what you are trying to solve, so we can help you on your way.






                    share|improve this answer





















                    • Sorry, I was not clear at all. "ModifyState -InputFile .temp.txt -Inactive -Active" was a try to understand what the function would return, but it's not what I want, because -Active and -Inactive must not given at the same time. Also -InputFile and -Object must not given at the same time What I want is: #Parameterset "I" ModifyState -Object PARAMVALUE -Inactive or ModifyState -InputFile PARAMVALUE -Inactive #Parameterset "A" ModifyState -Object PARAMVALUE -Active or ModifyState -InputFile PARAMVALUE -Active
                      – karnak
                      Nov 13 '18 at 16:11














                    0












                    0








                    0






                    With you example you shared with us, the following would be valid parametersets:



                    #Parameterset "I"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Inactive

                    #Parameterset "A"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Active


                    If you only plan on having those 2 parametersets and you have some parameters that should be valid for both, you actually don't need to declare them in the parametersets - unless the should behave differently.



                    So you example could be written like this:



                    function ModifyState (    
                    [string]$InputFile,
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }


                    If you plan on executing your function without any parameters, you need to provide a [CmdletBinding(DefaultParameterSetName = 'Default')] section and provide default values for the parameters.



                    function ModifyState {
                    [CmdletBinding(DefaultParameterSetName = 'A')]
                    param(
                    [string]$InputFile = "Filepath",
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active = [switch]::Present,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }
                    }


                    That would make it possible for you to run the function without parameters. The -Active will always be set and the -InputFile will have always point the to file you specify as the default value.



                    With all that in place - what would you have expected from the ModifyState -InputFile .temp.txt -Inactive -Active ?



                    Either you should be using:



                    ModifyState -InputFile .temp.txt -Inactive



                    or



                    ModifyState -InputFile .temp.txt -Active



                    with the current parametersets you have defined.



                    Please update you question with more details about what you are trying to solve, so we can help you on your way.






                    share|improve this answer












                    With you example you shared with us, the following would be valid parametersets:



                    #Parameterset "I"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Inactive

                    #Parameterset "A"
                    ModifyState -InputFile PARAMVALUE -Object PARAMVALUE -Active


                    If you only plan on having those 2 parametersets and you have some parameters that should be valid for both, you actually don't need to declare them in the parametersets - unless the should behave differently.



                    So you example could be written like this:



                    function ModifyState (    
                    [string]$InputFile,
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }


                    If you plan on executing your function without any parameters, you need to provide a [CmdletBinding(DefaultParameterSetName = 'Default')] section and provide default values for the parameters.



                    function ModifyState {
                    [CmdletBinding(DefaultParameterSetName = 'A')]
                    param(
                    [string]$InputFile = "Filepath",
                    [string]$Object,
                    [Parameter(Mandatory = $false, ParameterSetName = "A")]
                    [switch]$Active = [switch]::Present,
                    [Parameter(Mandatory = $false, ParameterSetName = "I")]
                    [switch]$Inactive) {
                    switch ($PsCmdlet.ParameterSetName) {
                    "A" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Activate $obj"
                    }
                    }
                    else {write-host "Activate $Object"}
                    }
                    "I" {
                    if ($InputFile) {
                    foreach ($obj in (gc $InputFile)) {
                    write-host "Deactivate $obj"
                    }
                    }
                    else {write-host "Deactivate $Object"}
                    }
                    }
                    }
                    }


                    That would make it possible for you to run the function without parameters. The -Active will always be set and the -InputFile will have always point the to file you specify as the default value.



                    With all that in place - what would you have expected from the ModifyState -InputFile .temp.txt -Inactive -Active ?



                    Either you should be using:



                    ModifyState -InputFile .temp.txt -Inactive



                    or



                    ModifyState -InputFile .temp.txt -Active



                    with the current parametersets you have defined.



                    Please update you question with more details about what you are trying to solve, so we can help you on your way.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 '18 at 19:58









                    Mötz

                    1,032711




                    1,032711












                    • Sorry, I was not clear at all. "ModifyState -InputFile .temp.txt -Inactive -Active" was a try to understand what the function would return, but it's not what I want, because -Active and -Inactive must not given at the same time. Also -InputFile and -Object must not given at the same time What I want is: #Parameterset "I" ModifyState -Object PARAMVALUE -Inactive or ModifyState -InputFile PARAMVALUE -Inactive #Parameterset "A" ModifyState -Object PARAMVALUE -Active or ModifyState -InputFile PARAMVALUE -Active
                      – karnak
                      Nov 13 '18 at 16:11


















                    • Sorry, I was not clear at all. "ModifyState -InputFile .temp.txt -Inactive -Active" was a try to understand what the function would return, but it's not what I want, because -Active and -Inactive must not given at the same time. Also -InputFile and -Object must not given at the same time What I want is: #Parameterset "I" ModifyState -Object PARAMVALUE -Inactive or ModifyState -InputFile PARAMVALUE -Inactive #Parameterset "A" ModifyState -Object PARAMVALUE -Active or ModifyState -InputFile PARAMVALUE -Active
                      – karnak
                      Nov 13 '18 at 16:11
















                    Sorry, I was not clear at all. "ModifyState -InputFile .temp.txt -Inactive -Active" was a try to understand what the function would return, but it's not what I want, because -Active and -Inactive must not given at the same time. Also -InputFile and -Object must not given at the same time What I want is: #Parameterset "I" ModifyState -Object PARAMVALUE -Inactive or ModifyState -InputFile PARAMVALUE -Inactive #Parameterset "A" ModifyState -Object PARAMVALUE -Active or ModifyState -InputFile PARAMVALUE -Active
                    – karnak
                    Nov 13 '18 at 16:11




                    Sorry, I was not clear at all. "ModifyState -InputFile .temp.txt -Inactive -Active" was a try to understand what the function would return, but it's not what I want, because -Active and -Inactive must not given at the same time. Also -InputFile and -Object must not given at the same time What I want is: #Parameterset "I" ModifyState -Object PARAMVALUE -Inactive or ModifyState -InputFile PARAMVALUE -Inactive #Parameterset "A" ModifyState -Object PARAMVALUE -Active or ModifyState -InputFile PARAMVALUE -Active
                    – karnak
                    Nov 13 '18 at 16:11


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268094%2fpowershell-parameter-in-more-than-one-parameterset-returns-ambigous-error%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini