Explain the Mandatory Attribute in PowerShell Advanced Function.

PowerShellMicrosoft TechnologiesSoftware & Coding

We have an example of PowerShell advanced function below and we will try to understand how mandatory parameter works.

function math_Operation{
   [cmdletbinding()]
   param([int]$val1,[int]$val2)
   Write-Host "Multiply : $($val1*$val2)"
   Write-Host "Addition : $($val1+$val2)"
   Write-Host "Subtraction : $($val1-$val2)"
   Write-Host "Divide : $($val1+$val2)"
}

When you execute the above example and don’t supply values then the script won’t ask you for the values, by default it will take the values and execute the program. See the execution below.

PS C:\WINDOWS\system32> math_Operation
Multiply : 0
Addition : 0
Subtraction : 0
Divide : 0

Even if you have mentioned two variables ($val1, $val2) and if you don’t supply them, the function doesn’t ask for the values and takes the null values. To make them necessary, use the Mandatory parameter as shown below.

function math_Operation{
   [cmdletbinding()]
   param([parameter(Mandatory=$True)]
      [int]$val1,
      [Parameter(Mandatory=$True)]
      [int]$val2)
   Write-Host "Multiply : $($val1*$val2)"
   Write-Host "Addition : $($val1+$val2)"
   Write-Host "Subtraction : $($val1-$val2)"
   Write-Host "Divide : $($val1+$val2)"
}

Output

PS C:\WINDOWS\system32> math_Operation
cmdlet math_Operation at command pipeline position 1
Supply values for the following parameters:
val1: 20
val2: 10
Multiply : 200
Addition : 30
Subtraction : 10
Divide : 30

As you can see in the output, when you mention the mandatory parameter, the script will ask the values of those variables when you execute without providing values. What happens when you don’t provide the value of the variables, does the mandatory parameter accepts the value? Yes, it does. It accepts the NULL values as default for integer variables. See the output below.

PS C:\WINDOWS\system32> math_Operation
cmdlet math_Operation at command pipeline position 1
Supply values for the following parameters:
val1:
val2:
Multiply : 0
Addition : 0
Subtraction : 0
Divide : 0

Please remember, above default value 0 is accepted for datatype Integer single variable but if you use the integer array (int[]) then PowerShell will not accept the empty array for example,

function print_number{
   [cmdletbinding()]
   param(
      [parameter(Mandatory=$True)]
      [int[]]$numbers
   )
   Write-Output "Integer Array"
   $numbers
}

Output 

PS C:\WINDOWS\system32> print_number
cmdlet print_number at command pipeline position 1
Supply values for the following parameters:
numbers[0]:
print_number : Cannot bind argument to parameter 'numbers' because it is an
empty array.
At line:1 char:1
+ print_number
+ ~~~~~~~~~~~~
   + CategoryInfo          : InvalidData: (:) [print_number], ParameterBindin
g
   ValidationException
   + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAll
o
   wed,print_number

But if you provide the empty input for a string value and a string array, they both don’t accept the empty or null strings.

Example

function print_String{
   [cmdletbinding()]
   param(
      [parameter(Mandatory=$True)]
      [string]$name,
      [parameter(Mandatory=$True)]
      [string[]]$names
   )
   $name
   $names
}

Output

PS C:\WINDOWS\system32> print_String
cmdlet print_String at command pipeline position 1
Supply values for the following parameters:
name:
print_String : Cannot bind argument to parameter 'name' because it is an empty
string.
At line:1 char:1
+ print_String
+ ~~~~~~~~~~~~
   + CategoryInfo          : InvalidData: (:) [print_String], ParameterBindin
g
   ValidationException
   + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
l
   owed,print_String

Conclusion of the above examples is when mandatory parameter specified, null, or Empty value acceptance depends on the datatype and none of the collections accept the null value.

raja
Published on 04-May-2020 07:00:08
Advertisements