Parameters in Functions

Hi All

I am relatively new to the powershell seen but i am keep to learn about it so i ave been watching videos and learning.

I currently want to create a script where i can run a certain {script block} depending on the parameter used in my function
e.g. i want something like

function set-stuff {

param(
$param1
$param2
$param3
)

if ($param1) {do this}
if ($param2) {do this}
if ($param3) {do this}

}

but i don’t want to declare the Variable values until one is chosen,can this be done and can anyone guide me

Thanks all in Advance

So … welcome to the brighter world of Powershell scripting! :wink:

but i don't want to declare the Variable values until one is chosen,can this be done and can anyone guide me
I guess you will have to explain that a little more. You cannot change the code of your function after you called it!?

i wondered if i could call a function with a param that has been chosen from a list, then once the param has been chosen get the user to give it a value so it runs a certain script block?

so you want to call a function with an empty variable and then prompt the user for a value from inside the function ?

yes is possible

Like most of the time: It depends. :wink: You have several options to make your function fit your needs. You could make your parameter mandatory - so the user has to provide a value. You could create different parameter sets - so the user has to chose one of it. Or you could … :wink:

It would be entirely possible, but perhaps not the best practiced thing to do. What is the use-case for this?

Could you provide a few examples of how you would call and use this function?

This is purley for learning purposes at the moment to see if i can do it, might be used a couple of times a month

If mandatory they would have to be declared even if the user doesn’t decide to use that param in the Function, which i do really want to do. ive not worked with parameter sets, can these be used to run differnt script blocks?

I can’t really be sure about requirements as your request is quite vague
You could create a ValidateSet and switch on that to do different things based on the input of $Do

function Do-Thing
{
    param(
        [Parameter(Mandatory = $true)]
        [ValidateSet('Thing1', 'Thing2', 'Thing3')]
        [string]$Do
    )

    switch($Do) {
        'Thing1' {
            Write-Output 'Doing Thing 1'
        }

        'Thing2' {
            Write-Output 'Doing Thing 2'
        }

        'Thing3' {
            Write-Output 'Doing Thing 3'
        }
    }
}

ok looks good so i would use
Do-Thing -Thing1 or -Thing2 or -Thing3?

function Do-Thing
{
param(
[Parameter(Mandatory = $true)]
[ValidateSet(‘Thing1’, ‘Thing2’, ‘Thing3’)]
[string]$Do
)

switch($Do) {
    'Thing1' {
        Write-Output 'Doing Thing 1'
    }

    'Thing2' {
        Write-Output 'Doing Thing 2'
    }

    'Thing3' {
        Write-Output 'Doing Thing 3'
    }
}

}

yes that would work

No, in that case you would use

Do-Thing -Do Thing1

For the functionality you’re describing you would use switch parameters

function Do-Thing
{
    param(
        [switch]$Thing1,

        [switch]$Thing2,

        [switch]$Thing3
    )

    if ($Thing1.IsPresent) {
        Write-Output 'Doing Thing 1'
    }

    if ($Thing2.IsPresent) {
        Write-Output 'Doing Thing 2'
    }

    if ($Thing3.IsPresent) {
        Write-Output 'Doing Thing 3'
    }
}
Do-Thing -Thing1 -Thing2 -Thing3

I played around a bit and your guidance was appreciated, now i got it to work how i want

Thanks