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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s