Richard's profileRichard Siddaway's BlogPhotosBlogLists Tools Help

Blog


    January 29

    CTP 3 – More functions

    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 

    Begin{}

    Process{}

    End{}

    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
    {
        [CmdletBinding()]
        Param ($computer=".")       #default to local machine
       Begin {
           Clear-Host
           # 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
            }
            else
            {
                Write-Host "Disk information for local host"  `n
            }

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

        }
        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

     

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!2016.trak
    Weblogs that reference this entry
    • None