Author Posts

March 3, 2017 at 2:35 pm

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?

March 3, 2017 at 2:46 pm

function get-selectedparams{

param($one,$two,$three)

$PSBoundParameters

}

March 3, 2017 at 3:36 pm

Idan,

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

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

March 3, 2017 at 4:50 pm

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.

March 3, 2017 at 5:16 pm

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/