Powershell Dynamic conditional parametersSet a default parameter value for a JavaScript functionDoes Java support default parameter values?How can I pass a parameter to a setTimeout() callback?Determine installed PowerShell versionHow to run a PowerShell scriptPowerShell says “execution of scripts is disabled on this system.”How to mark a switch parameter as mandatory in PowershellHow do you comment out code in PowerShell?How are parameters sent in an HTTP POST request?What are the -Xms and -Xmx parameters when starting JVM?

What is the maximum amount of diamond in one Minecraft game?

What is this airplane with small wings at different angles seen at Paphos Airport?

What's the big deal about the Nazgûl losing their horses?

Park the computer

Attach a visible light telescope to the outside of the ISS

Removing polygon holes in OpenLayers

Minor differences between two recorded guitars

PhD: When to quit and move on?

Is reasonable to assume that the 食 in 月食/日食 can be interpreted as the sun/moon being "eaten" during an eclipse?

How do I talk to my wife about unrealistic expectations?

Machine Learning Golf: Multiplication

Can you take the Dodge action while prone?

What is the highest level of accuracy in motion control a Victorian society could achieve?

Any way to meet code with 40.7% or 40.44% conduit fill?

Why no parachutes in the Orion AA2 abort test?

Do I need transit visa for Dublin?

Shipped package arrived - didn't order, possible scam?

Why does "sattsehen" take accusative "mich", not dative "mir"? Even though it is not "me" that I'm looking at?

How predictable is $RANDOM really?

Bringing coumarin-containing liquor into the USA

Initializing variables in an "if" statement

Is there a standard definition of the "stall" phenomena?

Will Jimmy fall off his platform?

Can a USB hub be used to access a drive from two devices?



Powershell Dynamic conditional parameters


Set a default parameter value for a JavaScript functionDoes Java support default parameter values?How can I pass a parameter to a setTimeout() callback?Determine installed PowerShell versionHow to run a PowerShell scriptPowerShell says “execution of scripts is disabled on this system.”How to mark a switch parameter as mandatory in PowershellHow do you comment out code in PowerShell?How are parameters sent in an HTTP POST request?What are the -Xms and -Xmx parameters when starting JVM?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am writing a script and I want to specify the parameters to do the following:



Parameter 1 is action (either check or kill)
Parameter 2 is computername.



If neither parameter is specified I want my Usage information displayed
Parameter 2 should ONLY be prompted if Parameter 1 is specified.



Param(
[Parameter(Mandatory=$True,
HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
[String]$Action,

[Parameter(Mandatory = $false,
Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
[ValidateNotNullorEmpty()]
[String]$Computers
)









share|improve this question




























    0















    I am writing a script and I want to specify the parameters to do the following:



    Parameter 1 is action (either check or kill)
    Parameter 2 is computername.



    If neither parameter is specified I want my Usage information displayed
    Parameter 2 should ONLY be prompted if Parameter 1 is specified.



    Param(
    [Parameter(Mandatory=$True,
    HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
    [String]$Action,

    [Parameter(Mandatory = $false,
    Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
    [ValidateNotNullorEmpty()]
    [String]$Computers
    )









    share|improve this question
























      0












      0








      0








      I am writing a script and I want to specify the parameters to do the following:



      Parameter 1 is action (either check or kill)
      Parameter 2 is computername.



      If neither parameter is specified I want my Usage information displayed
      Parameter 2 should ONLY be prompted if Parameter 1 is specified.



      Param(
      [Parameter(Mandatory=$True,
      HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
      [String]$Action,

      [Parameter(Mandatory = $false,
      Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
      [ValidateNotNullorEmpty()]
      [String]$Computers
      )









      share|improve this question














      I am writing a script and I want to specify the parameters to do the following:



      Parameter 1 is action (either check or kill)
      Parameter 2 is computername.



      If neither parameter is specified I want my Usage information displayed
      Parameter 2 should ONLY be prompted if Parameter 1 is specified.



      Param(
      [Parameter(Mandatory=$True,
      HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
      [String]$Action,

      [Parameter(Mandatory = $false,
      Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
      [ValidateNotNullorEmpty()]
      [String]$Computers
      )






      powershell parameters conditional






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 16:58









      Cleadus FetusCleadus Fetus

      485 bronze badges




      485 bronze badges






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Why force users to guess at what input is expected?
          Just tell them up front what is expected.



          For example:



          Function Test-DescriptiveUserPrompt

          [CmdletBinding()]

          Param
          (
          [ValidateSet('C','K')]
          [string]$Action = $(
          Write-Host '
          Please Enter an Action. (C)heck, (K)ill: ' -ForegroundColor Yellow -NoNewLine
          Read-Host
          ),

          [ValidateNotNullorEmpty()]
          [string[]]$Computers = $(
          Write-Host '
          Please Enter One or More Hostnames. separate multiple hostnames with a comma.
          EXAMPLE: Hostname1,Hostname2: ' -ForegroundColor Yellow -NoNewLine
          Read-Host
          )
          )

          Process

          "You choose $Action"
          "You enter the list $Computers"




          # Results

          Test-DescriptiveUserPrompt

          Please Enter an Action. (C)heck, (K)ill: c

          Please Enter One or More Hostnames. seperate multiple hostnames with a comma.
          EXAMPLE: Hostname1,Hostname2: localhost,remotehost
          c
          localhost,remotehost


          Test-DescriptiveUserPrompt -Action C -Computers localhost,remotehost

          C
          localhost
          remotehost





          share|improve this answer























          • This is excellent. Thank you.

            – Cleadus Fetus
            Mar 27 at 17:23











          • no worries, glad it can help you out.

            – postanote
            Mar 27 at 18:10


















          0














          The quick and dirty and simpler way of doing things is to use Parameter Sets. In this case, it will default to displaying the Usage information if things aren't correct.



          Function Test 
          [CmdletBinding()]
          Param(
          [Parameter(Mandatory=$true, ParameterSetName = "Action",
          HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
          [ValidateSet("C","K","?")]
          [Parameter(Mandatory=$false, ParameterSetName = "Usage")]
          [String]$Action,

          [Parameter(Mandatory = $true, ParameterSetName = "Action",
          Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
          [ValidateNotNullorEmpty()]
          [String]$Computers
          )
          Process

          if($PSCmdlet.ParameterSetName -eq "Usage" -or $Action -eq "?")

          Write-Host "Usage"

          else

          Write-Host "Action"
          Write-Host $Action
          Write-Host $Computers








          share|improve this answer

























            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%2f55342918%2fpowershell-dynamic-conditional-parameters%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









            1














            Why force users to guess at what input is expected?
            Just tell them up front what is expected.



            For example:



            Function Test-DescriptiveUserPrompt

            [CmdletBinding()]

            Param
            (
            [ValidateSet('C','K')]
            [string]$Action = $(
            Write-Host '
            Please Enter an Action. (C)heck, (K)ill: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            ),

            [ValidateNotNullorEmpty()]
            [string[]]$Computers = $(
            Write-Host '
            Please Enter One or More Hostnames. separate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            )
            )

            Process

            "You choose $Action"
            "You enter the list $Computers"




            # Results

            Test-DescriptiveUserPrompt

            Please Enter an Action. (C)heck, (K)ill: c

            Please Enter One or More Hostnames. seperate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: localhost,remotehost
            c
            localhost,remotehost


            Test-DescriptiveUserPrompt -Action C -Computers localhost,remotehost

            C
            localhost
            remotehost





            share|improve this answer























            • This is excellent. Thank you.

              – Cleadus Fetus
              Mar 27 at 17:23











            • no worries, glad it can help you out.

              – postanote
              Mar 27 at 18:10















            1














            Why force users to guess at what input is expected?
            Just tell them up front what is expected.



            For example:



            Function Test-DescriptiveUserPrompt

            [CmdletBinding()]

            Param
            (
            [ValidateSet('C','K')]
            [string]$Action = $(
            Write-Host '
            Please Enter an Action. (C)heck, (K)ill: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            ),

            [ValidateNotNullorEmpty()]
            [string[]]$Computers = $(
            Write-Host '
            Please Enter One or More Hostnames. separate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            )
            )

            Process

            "You choose $Action"
            "You enter the list $Computers"




            # Results

            Test-DescriptiveUserPrompt

            Please Enter an Action. (C)heck, (K)ill: c

            Please Enter One or More Hostnames. seperate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: localhost,remotehost
            c
            localhost,remotehost


            Test-DescriptiveUserPrompt -Action C -Computers localhost,remotehost

            C
            localhost
            remotehost





            share|improve this answer























            • This is excellent. Thank you.

              – Cleadus Fetus
              Mar 27 at 17:23











            • no worries, glad it can help you out.

              – postanote
              Mar 27 at 18:10













            1












            1








            1







            Why force users to guess at what input is expected?
            Just tell them up front what is expected.



            For example:



            Function Test-DescriptiveUserPrompt

            [CmdletBinding()]

            Param
            (
            [ValidateSet('C','K')]
            [string]$Action = $(
            Write-Host '
            Please Enter an Action. (C)heck, (K)ill: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            ),

            [ValidateNotNullorEmpty()]
            [string[]]$Computers = $(
            Write-Host '
            Please Enter One or More Hostnames. separate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            )
            )

            Process

            "You choose $Action"
            "You enter the list $Computers"




            # Results

            Test-DescriptiveUserPrompt

            Please Enter an Action. (C)heck, (K)ill: c

            Please Enter One or More Hostnames. seperate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: localhost,remotehost
            c
            localhost,remotehost


            Test-DescriptiveUserPrompt -Action C -Computers localhost,remotehost

            C
            localhost
            remotehost





            share|improve this answer













            Why force users to guess at what input is expected?
            Just tell them up front what is expected.



            For example:



            Function Test-DescriptiveUserPrompt

            [CmdletBinding()]

            Param
            (
            [ValidateSet('C','K')]
            [string]$Action = $(
            Write-Host '
            Please Enter an Action. (C)heck, (K)ill: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            ),

            [ValidateNotNullorEmpty()]
            [string[]]$Computers = $(
            Write-Host '
            Please Enter One or More Hostnames. separate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: ' -ForegroundColor Yellow -NoNewLine
            Read-Host
            )
            )

            Process

            "You choose $Action"
            "You enter the list $Computers"




            # Results

            Test-DescriptiveUserPrompt

            Please Enter an Action. (C)heck, (K)ill: c

            Please Enter One or More Hostnames. seperate multiple hostnames with a comma.
            EXAMPLE: Hostname1,Hostname2: localhost,remotehost
            c
            localhost,remotehost


            Test-DescriptiveUserPrompt -Action C -Computers localhost,remotehost

            C
            localhost
            remotehost






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 26 at 2:36









            postanotepostanote

            4,9102 gold badges4 silver badges11 bronze badges




            4,9102 gold badges4 silver badges11 bronze badges












            • This is excellent. Thank you.

              – Cleadus Fetus
              Mar 27 at 17:23











            • no worries, glad it can help you out.

              – postanote
              Mar 27 at 18:10

















            • This is excellent. Thank you.

              – Cleadus Fetus
              Mar 27 at 17:23











            • no worries, glad it can help you out.

              – postanote
              Mar 27 at 18:10
















            This is excellent. Thank you.

            – Cleadus Fetus
            Mar 27 at 17:23





            This is excellent. Thank you.

            – Cleadus Fetus
            Mar 27 at 17:23













            no worries, glad it can help you out.

            – postanote
            Mar 27 at 18:10





            no worries, glad it can help you out.

            – postanote
            Mar 27 at 18:10













            0














            The quick and dirty and simpler way of doing things is to use Parameter Sets. In this case, it will default to displaying the Usage information if things aren't correct.



            Function Test 
            [CmdletBinding()]
            Param(
            [Parameter(Mandatory=$true, ParameterSetName = "Action",
            HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
            [ValidateSet("C","K","?")]
            [Parameter(Mandatory=$false, ParameterSetName = "Usage")]
            [String]$Action,

            [Parameter(Mandatory = $true, ParameterSetName = "Action",
            Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
            [ValidateNotNullorEmpty()]
            [String]$Computers
            )
            Process

            if($PSCmdlet.ParameterSetName -eq "Usage" -or $Action -eq "?")

            Write-Host "Usage"

            else

            Write-Host "Action"
            Write-Host $Action
            Write-Host $Computers








            share|improve this answer



























              0














              The quick and dirty and simpler way of doing things is to use Parameter Sets. In this case, it will default to displaying the Usage information if things aren't correct.



              Function Test 
              [CmdletBinding()]
              Param(
              [Parameter(Mandatory=$true, ParameterSetName = "Action",
              HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
              [ValidateSet("C","K","?")]
              [Parameter(Mandatory=$false, ParameterSetName = "Usage")]
              [String]$Action,

              [Parameter(Mandatory = $true, ParameterSetName = "Action",
              Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
              [ValidateNotNullorEmpty()]
              [String]$Computers
              )
              Process

              if($PSCmdlet.ParameterSetName -eq "Usage" -or $Action -eq "?")

              Write-Host "Usage"

              else

              Write-Host "Action"
              Write-Host $Action
              Write-Host $Computers








              share|improve this answer

























                0












                0








                0







                The quick and dirty and simpler way of doing things is to use Parameter Sets. In this case, it will default to displaying the Usage information if things aren't correct.



                Function Test 
                [CmdletBinding()]
                Param(
                [Parameter(Mandatory=$true, ParameterSetName = "Action",
                HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
                [ValidateSet("C","K","?")]
                [Parameter(Mandatory=$false, ParameterSetName = "Usage")]
                [String]$Action,

                [Parameter(Mandatory = $true, ParameterSetName = "Action",
                Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
                [ValidateNotNullorEmpty()]
                [String]$Computers
                )
                Process

                if($PSCmdlet.ParameterSetName -eq "Usage" -or $Action -eq "?")

                Write-Host "Usage"

                else

                Write-Host "Action"
                Write-Host $Action
                Write-Host $Computers








                share|improve this answer













                The quick and dirty and simpler way of doing things is to use Parameter Sets. In this case, it will default to displaying the Usage information if things aren't correct.



                Function Test 
                [CmdletBinding()]
                Param(
                [Parameter(Mandatory=$true, ParameterSetName = "Action",
                HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
                [ValidateSet("C","K","?")]
                [Parameter(Mandatory=$false, ParameterSetName = "Usage")]
                [String]$Action,

                [Parameter(Mandatory = $true, ParameterSetName = "Action",
                Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
                [ValidateNotNullorEmpty()]
                [String]$Computers
                )
                Process

                if($PSCmdlet.ParameterSetName -eq "Usage" -or $Action -eq "?")

                Write-Host "Usage"

                else

                Write-Host "Action"
                Write-Host $Action
                Write-Host $Computers









                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 20:27









                HAL9256HAL9256

                5,41315 silver badges32 bronze badges




                5,41315 silver badges32 bronze badges



























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342918%2fpowershell-dynamic-conditional-parameters%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







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript