Powershell Functions with parameter binding

The Script 😉

Code is simple and self-explanatory

function f1
{
param($argone, $argtwo)
begin{
        # Only gets process at the Beginning
        # Normally include Variable initialization
    if($argone -eq $null -or $argtwo -eq $null)
    {
        Write-Host "`nFunction called with 0 args"
        $argone = "Grape"
        $argtwo = "Mango"
    }
    else
    {
        Write-Host "`nFunction called with args"
    }
}
Process{
        # Gets process for each object in the pipe 
        "`t{0} is arg one and {1} is arg two" -f $argone,$argtwo
}
End{
        # Always get processed once at the end
        Write-Host "Exiting function"
}
}
cls
#Fucntion call with parameter binding
f1 -argone "Apple" -argtwo "Orange"
#Fucntion call with parameter binding in revese
f1 -argtwo "Apple" -argone "Orange"
#Fucntion call with out parameters 
f1
Output
Function called with args
	Apple is arg one and Orange is arg two
Exiting function
Function called with args
	Orange is arg one and Apple is arg two
Exiting function
Function called with 0 args
	Grape is arg one and Mango is arg two
Exiting function

Leave a comment

Your email address will not be published. Required fields are marked *

WordPress Appliance - Powered by TurnKey Linux