WIDBA

WIDBA - The busiest DBA in Wisconsin, its all I do and I do it well

Tuesday, March 1, 2011

Looking for trouble in all the right places with Powershell - Drive Space

In continuing with my series on Powershell monitoring (Post 1), I will move on to one of the more prevalent examples out there, collecting drive space information.  This function looks for any drives that have less free space than a factor I pass in.  When doing monitoring on several servers, you really need to keep the output to the problems, this can easily be changed to create an inventory if needed.  This particular script will not work with mount points as I have read.  Someday I will have some time to create one that does both.  I learned from several others on this by reading and changed up to create my own, particularly so I could change the multiplier for specific servers if I so chose.   


function Get-DriveSpace
{
    param( [Parameter(Mandatory=$true)]$computer , [int]$thresholdMultiplier=.10 )
    [string]$filter = "DriveType = '3'"

    $wmi = Get-WmiObject -class win32_logicaldisk -filter $filter -comp $computer |
        Where-Object {$_.size -gt 0} |   
        Where-Object {$_.freespace -le ($_.size * $thresholdMultiplier)} |
        Select-Object Name,Size,Freespace
    return $wmi;
    }

Note the line Where-Object {$_.freespace -le ($_.size * $thresholdMultiplier)} .  This is where I want to only return drives that have limited space available.

# Here is a sample call that you can add to your loop for each server to monitor
try{
        Get-DriveSpace -computer $server | FT -AutoSize
        } catch { Write-Host "Drive Space Error:$server-" $_  }


Well, that is it.  Not a particularly complex function and its widely published, but the end game is to provide a complete set of functions you can use to create a useful monitoring script in your environment.  Next up is starting to learn to collect perfmon counters and store them, all from a central script.

No comments:

Post a Comment