checking which parameter selected

Welcome Forums General PowerShell Q&A checking which parameter selected

This topic contains 4 replies, has 3 voices, and was last updated by

 
Moderator
1 year, 8 months ago.

  • Author
    Posts
  • #65721

    Participant
    Points: 0
    Rank: Member

    hi
    i have a script that has several parameters.
    A user selects a parameter and gives it a value.
    When parameter is selected a certain cmdlet is being called.
    each parameter has different a action.

    currently i'm using an IF statement for each parameter to check if it was selected.
    is there a better way to check which parameter is selected?
    also, i want a "default" cmdlet to take place when no parameter (they're not mandatory) is selected, is there a nice way to check this too?

  • #65728

    Participant
    Points: 21
    Rank: Member

    function get-selectedparams{

    param($one,$two,$three)

    $PSBoundParameters

    }

  • #65745

    Moderator
    Points: 24
    Team Member
    Rank: Member

    Idan,

    The following short post gives a quick explanation of $PSBoundParameters.

    http://tommymaynard.com/quick-learn-the-psboundparameters-automatic-variable-2016/

  • #65752

    Participant
    Points: 0
    Rank: Member

    Thank you Daniel for the link.
    this is basically what i did in my script.
    I was just wondering if maybe there is a better way to work with the parameter selection besides using if statement for each one.

  • #65761

    Moderator
    Points: 24
    Team Member
    Rank: Member

    Apologies, I didn't read your original post fully because I wanted to add context to Dan's reply.

    An alternative way to test for parameters is using parameter sets which would give you an idea for your question regarding defaults.

    Here are some links as reference:

    PowerShell V2: ParameterSets


    http://blog.simonw.se/powershell-functions-and-parameter-sets/

The topic ‘checking which parameter selected’ is closed to new replies.