Explain the PowerShell Advanced Function.

PowerShellMicrosoft TechnologiesSoftware & Coding

Before starting the Advance PowerShell function, assuming we know about the PowerShell function. You can check the explanation on the PowerShell function below.

https://www.tutorialspoint.com/explain-the-powershell-function

Here, we will take the math function example that calculates the different types of operations. We already have a code with the simple function as shown below.

function math_Operation{
   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)"
}

The above example is of the simple function. When you run the above code and run the function from the terminal and you can notice you will see only the $val1 and $val2 parameters while the PowerShell advanced function contains the additional common parameters like ErrorAction, WarningAction, Verbose, Passthru, etc.

Check the syntax below for the function when it is used as a simple function. No common parameters are added.

PS C:\WINDOWS\system32> Get-Help math_Operation
NAME
   math_Operation
SYNTAX
   math_Operation [[-val1] <int>] [[-val2] <int>]
ALIASES
   None
REMARKS
   None

To convert the simple function into the advanced function we just need to use the [cmdletbinding] into the function.

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)"
}

Now execute the above code and check the parameters, you can see the other parameters referred to as common parameters. Check the Syntax of this function after the execution of the code.

PS C:\WINDOWS\system32> Get-Help math_Operation
NAME
   math_Operation
SYNTAX
   math_Operation [[-val1] <int>] [[-val2] <int>] [<CommonParameters>]
ALIASES
   None
REMARKS
   None

This does not end here. PowerShell advanced function is more. So far we have converted the simple function into an advanced function. Let’s check the structure of the advanced function.

Advance function structure

function Verb-Noun {
   [CmdletBinding()]
   param (
      // Parameters to declare with their datatypes
   )
   begin {
      // Initialization of variables, create a log file
   }
   process {
      // Program code to make this function work
   }
   end {
      // Clearing values, log files, etc.
   }
}

In the above function, you can see the PowerShell Advanced Function is mainly comprised of 3 blocks (Begin, Process, and End).

  • In the Begin block, you need to initialize the value of the variable or need to declare the log files, etc. This block executes first before the other two blocks and executes once only.

  • The Process block, where the actual code is executed and which uses the parameters declared in Param block and the values and log file path initialized in Begin block. This block runs for each item in the block.

  • The End block contains the values to be released/cleared from the variables and other cleanup tasks. This block also executes one time only after the process block executed.

Apart from the above functionalities, many other parameter attributes and arguments are supported which are explained in the next subsequent articles. Just have a glance at those parameter attributes and arguments.

Parameter Arguments

  • Mandatory

  • Parameter=0

  • ValueFromPipeline

  • ValueFromPipelineByPropertyName

  • ValueFromRemainingArguments

  • HelpMessage

Parameter Attributes

  • Param

  • Parameter

  • [AllowNull()]

  • [AllowEmptyString()]

  • [AllowEmptyCollection()]

  • [ValidateCount()]

  • [ValidateLength()]

  • [ValidatePattern()]

  • [ValidateRange()]

  • [ValidateScript()]

  • [ValidateSet()]

  • [ValidateNotNull()]

  • [ValidateNotNullOrEmpty()]

  • [DynamicParam]

  • [Switch]

raja
Published on 04-May-2020 06:55:53
Advertisements