Parameters in Functions

This topic contains 14 replies, has 4 voices, and was last updated by  Ashley 3 months, 2 weeks ago.

  • Author
    Posts
  • #78514

    Ashley
    Participant

    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

  • #78515

    Olaf Soyk
    Participant

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

    • #78517

      Ashley
      Participant

      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?

  • #78520

    Simon B
    Participant

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

    • #78521

      Ashley
      Participant

      yes is possible

    • #78526

      Liam Fisher
      Participant

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

    • #78529

      Ashley
      Participant

      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

    • #78527

      Liam Fisher
      Participant

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

  • #78524

    Olaf Soyk
    Participant

    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 .............................. 😉

    • #78532

      Ashley
      Participant

      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?

  • #78535

    Liam Fisher
    Participant

    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'
            }
        }
    }
    
    • #78541

      Ashley
      Participant

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

    • #78544

      Liam Fisher
      Participant

      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
      
    • #78566

      Ashley
      Participant

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

      Thanks

  • #78542

    Simon B
    Participant

    yes that would work

You must be logged in to reply to this topic.