DaveWentzel.com            All Things Data

Passing Parameters

You can use:

Arguments

...are unnamed parameters.  

$args[i]

...and are indexed at 0. 

Here is an example showing how to pass either variable args, array args, or a combination, to a function:

cls

function FunctionThatTakesArrays

{

    
If (!$args)

    
{

        
# you can specify arrays or standard arguments as arguments, or mix and match

        # depending on your purpose the function can then call itself recursively

        # using each array element as an argument to itself.  

        
"No arguments passed."

    
}

    ElseIf
($args[0] -is [array])

    
{

        
Foreach ($ArrayElement in $args[0])

        
{

          
"$ArrayElement is an array element"

            
FunctionThatTakesArrays $ArrayElement

        }

    }

    
Else

    
{

        '$args: ' + $args

    }

}


"Call 1"

FunctionThatTakesArrays  #no args passed

"Call 2"

FunctionThatTakesArrays Dave  #no array passed

"Call 3"

FunctionThatTakesArrays Dave,Angie  #this is an array

"Call 4"

FunctionThatTakesArrays "Dave,Angie" #this is not an array

"Call 5"

FunctionThatTakesArrays "Dave,Angie" "Jackson Natalie"  #not an array

"Call 6"

FunctionThatTakesArrays "Dave,Angie","Jackson Natalie"  #is an array


 
Call 1
No arguments passed.
Call 2
$args: Dave
Call 3
Dave is an array element
$args: Dave
Angie is an array element
$args: Angie
Call 4
$args: Dave,Angie
Call 5
$args: Dave,Angie Jackson Natalie
Call 6
Dave,Angie is an array element
$args: Dave,Angie
Jackson Natalie is an array element
$args: Jackson Natalie

 

 

 

Params

Basic syntax:
Param ([String]$FileName)
 
Full syntax:

param(

    
[Parameter(

        Mandatory = $true,

        Position = 0,

        ValueFromPipeline = $true,

        ValueFromPipelineByPropertyName = $true)]

    [string[]] $Name


)

param(

    
[ValidateLength(5,10)]

    [string] $Name


A slightly different syntax:

cls

Function MandatoryFunctionParameter
($Param=$(Throw "Param was not specified."))

{

    $Param

}

MandatoryFunctionParameter Dave

MandatoryFunctionParameter
Dave
Param was not specified.
At :line:2 char:52
+ Function MandatoryFunctionParameter ($Param=$(Throw  <<<< "Param was not specified."))

 

Mixing Arguments and Parameters

cls

# Named arguments/parameters

# Note that named arguments are still not mandatory and "extra" parameters

# are ignored (actually they are sent to $args)

# the parameter defaults are optional.
  

Function Add2Numbers
([int]$Number1=1, [int]$Number2=1)

{

    $Number1
+ $Number2

    $args

    
# you can also do something like

    # return $Number1 + $Number2

    # but return is not mandatory and a function can actually return more than one output

    # (like above)

}

Add2Numbers 1 2

Add2Numbers 4

Add2Numbers 10 11 12 13

Add2Numbers
-Number2 100 -Number1 1

Add2Numbers Dave Angie

Add2Numbers 1.1 2.2  #this isn't correct
3
5
21
12
13
101
Add2Numbers : Cannot process argument transformation on parameter 'Number1'. Can
not convert value "Dave" to type "System.Int32". Error: "Input string was not in
 a correct format."
At F:\Untitled4.ps1:19 char:12
+ Add2Numbers <<<<  Dave Angie 
    + CategoryInfo          : InvalidData: (:) [Add2Numbers], ParameterBindin.. 
   .mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add2Numbers
 
3

Advanced Validation

[ValidateSet("First Option", "Second Option", ..., "Last Option")]

 
...ensures that input to this parameter matches one of the options in the set.  PoSH uses the equivalent of -eq when comparing using this method.  If you have custom or non-standard validation you must do then simply write a function that does the necessary validation and call that.  
 

Add new comment