Author Posts

August 31, 2017 at 12:48 pm

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

August 31, 2017 at 12:56 pm

So ... welcome to the brighter world of Powershell scripting! 😉

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!?

August 31, 2017 at 1:08 pm

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?

August 31, 2017 at 1:14 pm

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

August 31, 2017 at 1:17 pm

yes is possible

August 31, 2017 at 1:17 pm

Like most of the time: It depends. 😉 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 .............................. 😉

August 31, 2017 at 1:20 pm

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

August 31, 2017 at 1:21 pm

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

August 31, 2017 at 1:23 pm

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

August 31, 2017 at 1:26 pm

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?

August 31, 2017 at 1:28 pm

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'
        }
    }
}

August 31, 2017 at 1:32 pm

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'
}
}
}

August 31, 2017 at 1:35 pm

yes that would work

August 31, 2017 at 1:37 pm

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

August 31, 2017 at 2:59 pm

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

Thanks