Monthly Archive

Categories

CTP3 – Advanced Functions

If you have been following the information released about CTP3 one of the things that will quickly become obvious is that something called Advanced Functions has been introduced. We now have functions and advanced functions in PowerShell.

For those of you who tried out CTP 2 you would have been aware of script cmdlets. Script cmdlets enabled cmdlets to be written in PowerShell rather than having to use C# and compile the cmdlet.

Advanced functions have replaced script cmdlets.

There is quite a lot to advanced functions so I am going to take a few posts to explore them fully.  We start by quickly reviewing PowerShell functions.

This is a function that I wrote a  long time ago when PowerShell was still in beta.

function Get-DiskFreeSpace
{
    Param ($computer=".")       #default to local machine

# get current foreground colour
    $origFg = $host.ui.rawui.foregroundColor     # get original foreground colour

#get the disk information
    $diskinfo = Get-WmiObject Win32_LogicalDisk -computer $computer

    Clear-Host
    if ($computer -ne ".")
    {
        Write-Host "Disk information for "  $computer  `n
    }
    else
    {
        Write-Host "Disk information for local host"  `n
    }

# set title line
    "{0,2}  {1,12}  {2,9}  {3,9}  {4,9}  {5,9} " -f "Drive", "Type", "Size",  "Used",  "Avail", "Use%"
    foreach ($disk in $diskinfo)
    {
        $size = $disk.Size / 1GB
        $strSize = $size.ToString("F") + "GB"       # format disk size string   
        $avail = $disk.FreeSpace / 1GB
        $strAvail = $avail.ToString("F") + "GB"    # format space available string   
        $used = $size - $avail
        $strUsed = $used.ToString("F") + "GB"    # format space used string
        if ($size -gt 0.0)
        {
            $useperc = ($used / $size) * 100.0
            $strUseperc = $useperc.ToString("F")    # format space available string
        }
        else
        {
            $strUseperc = "0.00"
        }
# set foreground colour based on percentage free space       
        switch ($useperc)
        {
            {$_ -ge 90.0} {$host.ui.rawui.foregroundColor = "Red"}
            {$_ -lt 90.0 -and $_ -ge 80.0} {$host.ui.rawui.foregroundColor = "Yellow"}
            {$_ -lt 80.0} {$host.ui.rawui.foregroundColor = "Green"}
            default {$host.ui.rawui.foregroundColor = "White"}
        }

# format output string       
        "{0,2}  {1,15}  {2,9}  {3,9}  {4,9}  {5,9} " -f $disk.Name, $disk.FileSystem,    $strSize, $strUsed, $strAvail, $strUseperc
    }
    $host.ui.rawui.foregroundColor = $origFg   # reset foreground colour
}
set-alias df get-diskfreespace

This simply loops through the disks connected to the machine and displays the drive, the type, the size, space used, the space available and the percentage of usage.

We turn this onto an advanced function by  adding the [CmdletBinding] attribute.  Our function becomes

#Requires -Version 2.0
function Get-DiskFreeSpace
{
    [CmdletBinding()]
    Param ($computer=".")       #default to local machine

lots of stuff as before

}

The #Requires tells PowerShell that we have to be running on version 2.  If you try running on version 1 it will fail.

[CmdletBinding()] now defines that we have an advanced function that can be treated as a cmdlet.  What we can do with that & some of the other function enhancements we will see as we progress through rewriting this function to take advantage of the changes in PowerShell v2.  It is very important to note that the function as originally written still runs very happily in V2.

Leave a Reply