Quantcast
Channel: Powershell – TheITBros
Viewing all articles
Browse latest Browse all 91

PowerShell Function Parameters: How to Add?

$
0
0
Adding Parameters to PowerShell Function

When developing your PowerShell functions, sometimes you need to put some input values to your functions, such as a file name, string, or any other value. In PowerShell, there are two ways to pass parameters to functions: through the $Args variable and by setting formal parameters.

Passing Parameters to the PoSh Function Through the $Args Variable

Let’s look at the simplest PowerShell function example that displays some text on screen:

Function TestPoShArgs { 

“I love PowerShell”

}

Add the $Args variable to the function:

Function TestPoShArgs { 

“I love PowerShell $Args”

}

And run it with a few parameters:

TestPoShArgs One Two Three

powershell function parameters

In this example, we passed 3 parameters to the function input, separated by spaces. At the output of the function, they also appear with spaces. You can change the output delimiter using the special variable $OFS.

Function TestPoShArgs { 

$OFS=","

“I love PowerShell $Args”

}

TestPoShArgs One Two Three

Now the returned values in the function results are separated by commas.

powershell function parameter

The $Args variable is a simple PowerShell array and you can access its individual values by sequence numbers (starting from 0).

Function TestPoShArgs { 

"Total args number: $($Args.Count)" 

For($i=0; $i -lt $Args.Count; $i++) {$Args[$i]}

}

TestPoShArgs One Two Three Four

As you can see, the function calculated the number of arguments that were passed to it and listed them line by line.

powershell function param

Passing Formal Parameters to PowerShell Functions

As in other programming languages, you can set a number of formal parameters for PowerShell functions. During the function execution, the values of these parameters will be replaced by the arguments passed by user.

Formal parameters are passed in parentheses after the function name:

Function TestPoShParam ($curwidth, $curheight) { 

 $summ=$curwidth+$curheight

 “Current summ $summ"

}

TestPoShParam 800 600

This function accepts two parameters as input and displays their sum.

powershell function arguments

When specifying function arguments, you can specify parameter names; in this case, their order doesn’t matter:

TestPoShParam -curheight 800 -curwidth 800

By default, PowerShell itself tries to determine the type of value in the parameter. But you can specify the type of the variable manually. For example:

Function TestPoShParam ([int] $curheight_meter, [string] $curname) {

“The mountain " + $curname + " is about " + ($curheight_meter/1000) +" km”

}

TestPoShParam 8848 'Everest' 

powershell function default parameter

If you swap parameters with the given types, Powershell functions will return an error:

TestPoShParam : Cannot process argument transformation on parameter ‘curheight_meter’. Cannot convert value

“Everest” to type “System.Int32”. Error: “Input string was not in a correct format.”

Also in the PowerShell function, you can specify that some parameters are mandatory required and their order. The following syntax is used for this:

Function TestPoShParam { 

PARAM (

 [PARAMETER(Mandatory=$True,Position=0,HelpMessage ="Specify full DB path")][ValidateLength(1,20)][String]$DBPath,

 [PARAMETER(Mandatory=$True,Position=1,HelpMessage ="Specify max DB size")][String]$MaxSize,

 [PARAMETER(Mandatory=$False,Position=2)][String]$Type='MSSQL'

)

 Return("Path:" +$DBPath + " DB_size:" + $maxsize + " DB_type:" + $Type) 




}

TestPoShParam -DBPath 'c:\mysql\' -MaxSize 20

powershell function parameter types

In this example, additional arguments appeared near the Parameter switch:

  • Mandatory – if True is specified, this function parameter is strongly required;
  • HelpMessage – help for the user on the function parameter;
  • Position – position of the argument when calling the function (Parameters must be specified in a strictly defined order. If you don`t specify one of the parameters, then PowerShell will automatically remind you of this and offer to enter the missing information);
  • ValidateLength – maximum argument length (you cannot use a value with a length that is greater than the specified argument limit).

The post PowerShell Function Parameters: How to Add? appeared first on TheITBros.


Viewing all articles
Browse latest Browse all 91

Trending Articles