Multi-Value Parameter Defaults

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Geraldo Quezada Geraldo Quezada 8 months, 3 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #33636
    Profile photo of Geraldo Quezada
    Geraldo Quezada
    Participant

    I would like to know if there is a way to assign more than one value to a parameter definition in and advanced function

     
    function set-networkinfo {
       [CmdLetBinding()]
    
       param (
          
          [Parameter()]
          [string[]]$DNSServers = '1.1.1.1', '2.2.2.2'
    
       )
    
    }
    
    

    I know this throws and invalid expression assignment and I can possibly separate into two distinct parameters with one value each. But I am just curious if this is even possible.

    #33637
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    Try it like this:

    [string[]]$DNSServers = @('1.1.1.1','2.2.2.2')

    The @() is called the array operator and is used to create an array of objects. Your parameter definition accepts an array of strings as denoted by the [].

    #33638
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You don't always have to use the @() operator to create arrays, but in this case, it's necessary to help the PowerShell parser know what's going on (because you normally have a comma separating parameters in the param block).

    #33639
    Profile photo of Geraldo Quezada
    Geraldo Quezada
    Participant

    Great that worked.. Thank you Matt and Dave

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.