header image

CTP 3 – More functions

Posted by: | January 29, 2009 | No Comment |

I realised after my post on advanced functions that I am getting ahead of myself.  Part of what I wanted to do was take the functional code in the original script and wrap it up to make it moe production orientated.  I suspect like many I tend to write scripts to do a job and they then are forgotten about as I move to the next task.  With some of the changes in PowerShell v2 – especially modules and the changes to functions it is time to think about rectifying this.

One thing with v2 functions is that they have 




script blocks available.  They have the same meaning as in foreach in that begin is processed once at the start, process is used at each call and end is called once when the function has finished. If you are just using it once it probably doesn’t matter but if you are going to use it on the pipeline then it does make processing more efficient.  I haven’t made much use of them before but now is the time to start thinking about how they can be used to improve the logic and readability of the function

This makes our function look like this

#Requires -Version 2.0
function Get-DiskFreeSpace
    Param ($computer=".")       #default to local machine
   Begin {
       # get current foreground colour
       $origFg = $host.ui.rawui.foregroundColor     # get original foreground colour

        # set title line
       $title = "{0,2}  {1,12}  {2,9}  {3,9}  {4,9}  {5,9} " -f "Drive", "Type", "Size",  "Used",  "Avail", "Use%"

    Process {
        #get the disk information
        $diskinfo = Get-WmiObject Win32_LogicalDisk -computer $computer
        if ($computer -ne ".")
            Write-Host "Disk information for "  $computer  `n
            Write-Host "Disk information for local host"  `n

        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
                $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

    End {
       $host.ui.rawui.foregroundColor = $origFg   # reset foreground colour
set-alias df get-diskfreespace

I have changed the begin, process and end key words to bold so they are obvious.  Next time we will look at getting this working on the pipeline


under: PowerShellV2