Home > Uncategorized > Powershell, advanced functions

Powershell, advanced functions

According to MSDN advanced functions (or script cmdlets) are defined as

Advanced functions allow you to write functions that can perform operations 
that are similar to the operations you can perform with cmdlets. Advanced 
functions are helpful when you want to quickly write a function without 
having to write a compiled cmdlet using a Microsoft .NET Framework 
language. These functions are also helpful when you want to restrict the
functionality of a compiled cmdlet or when you want to write a function 
that is similar to a compiled cmdlet.

    Advanced functions are defined using the CmdletBinding attribute. The attribute has four public properties:

    • ConfirmImpact
    • SupportsShouldProcess
    • SupportsTransactions
    • DefaultParameterSetName

    I think that, in most cases, the defaults are used and in this case an advanced function declaration might look like:

    function Increment()
    {
        [CmdletBinding()]
        param([Parameter(Position=0, Mandatory=$True)]$value)
        return $value + 1
    }

    This function accepts a mandatory parameter called value, which can either be specified by name:

    Increment -Value 10

    Or by position (0):

    Increment 10

    Okay, that was simple. Lets extend our increment function:

    function Increment()
    {
        [CmdletBinding()]
        param(
            [Parameter(
                Position=0,
                Mandatory=$True,
                ValueFromPipeline=$True,
                ValueFromPipelineByPropertyName=$True)]
            [int[]]$value,
            [Parameter(
                Position=1,
                Mandatory=$False,
                ValueFromPipelineByPropertyName=$True)]
            [Alias(‘delta’)]
            [int]$step = 1
            )

        begin
        {
            # $psBoundParameters is a hashtable containing the parameters
            # that were bound and what values were bound to them. 
            $inputFromPipeline = !$psBoundParameters.containskey(‘value’)
        }

        process
        {
            if ($inputFromPipeline)
            {
                $value[0] + $signedStep
            }
            else
            {
                $res = @()
                foreach($v in $value)
                {
                    $res += $v + $signedStep
                }
                $res
            }
        }

        end
        {
        }
    }

    This function can be called in a lot of different ways:

    # Sample usage: Positional argument
    Increment 10 2
    Increment 10,20,30 2

    # Sample usage: Named argument
    Increment -Value 10 -Step 2
    Increment -Value 10,20,30 -Step 2

    # Sample usage: Default values
    Increment -Value 10

    # Sample usage: Aliased argument names
    Increment -Value 10 -Delta 2

    # Sample usage: Value from pipeline
    10 | Increment -Step 2
    10,20,30 | Increment -Step 2

    # Sample usage: Value from pipeline by property name
    $obj = new-object psobject -property @{ Value = 13; Step=2 }
    $obj | Increment

    Almost overwhelming… but powerful (of course).

    Advertisements
    Categories: Uncategorized
    1. No comments yet.
    1. No trackbacks yet.

    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

    %d bloggers like this: