SQLvariations: SQL Server, a little PowerShell, maybe some Hyper-V Rotating Header Image

Quick Blog: PowerShell Disk and MountPoint Check

I tuned into twitter yesterday for a couple of minutes and found a great conversation going on between Nicolas Cain ( blog | twitter ) and Dave Levy ( blog | twitter ) about checking Disk Space & Mount Points. I really like what they were working on because it was actually one of my top priorities for the week. I already have some code for checking both regular disk drives and mount points with PowerShell but I was looking to improve it and get it ready for production monitoring.

Everyone’s environment is different and they build things based on needs and pain points. For my own environment I took a little of what Dave put together and a little of what Nick put together and built my own function for my environment. I’m still trying to add some more information to it but here’s what I’ve got so far:

Function Get-DisksSpace ([string]$Servername, $unit= "GB")
{
$measure = "1$unit"

Get-WmiObject -computername $serverName -query "
select SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label
  from Win32_Volume
 where DriveType = 2 or DriveType = 3" `
| select SystemName `
        , Name `
        , @{Label="SizeIn$unit";Expression={"{0:n2}" -f($_.Capacity/$measure)}} `
        , @{Label="FreeIn$unit";Expression={"{0:n2}" -f($_.freespace/$measure)}} `
        , @{Label="PercentFree";Expression={"{0:n2}" -f(($_.freespace / $_.Capacity) * 100)}} `
        ,  Label
}#Get-DisksSpace

The code above will create a PowerShell function (sorta kinda like a stored procedure only not really but just think of it like that if you’re a SQL person that’s new to PowerShell Smile ) Here’s how you would call it:

Get-DisksSpace “Win7NetBook” | Format-Table

Or if you only wanted to know about the drives that are low on space:

Get-DisksSpace “Win7NetBook” | where{$_.PercentFree -lt 20} | Format-Table

This will return you the list of drives and mount points on the machine you listed and default the unit of measure to convert the results to gigabytes. The great news is that this code runs really fast. Their conversation yesterday literally saved me hours of work. I’ll keep working with this and post another blog when I put the monitoring portion into production.

4 Comments

  1. Tom Hoover says:

    My profile as use for ISE is last in this comment.

    I cannot get the Get-DisksSpace to function. I have called it using

    Get-DisksSpace ‘computername’
    Get-DisksSpace “computername”
    Get-DisksSpace computername
    Get-DisksSpace $env:computername

    And it returns the error
    Get-WmiObject : Invalid class
    At C:\Documents and Settings\thoover\My Documents\WindowsPowerShell\Microsoft.PowerShellI
    SE_profile.ps1:9 char:14
    + Get-WmiObject <<< ”
    }

    Thanks for whatever help you can give me.

    1. Aaron Nelson says:

      Tom,

      Try running: Get-Command Get-DisksSpace and see if that returns anything. If it doesn’t that means the function didn’t load up. If it does respond back that it has Get-DisksSpace loaded up then that’s a completely different problem.

  2. […] If you’d like some of the scripts that I reference in my profile you can grab the Get-DisksSpace one here and I’ll be blogging about the rest later this week.  That’s all that I’ve […]

  3. […] This post was mentioned on Twitter by ravikanth and Nicholas Cain, Aaron Nelson. Aaron Nelson said: New post: Quick Blog: PowerShell Disk and MountPoint Check http://bit.ly/fXC0qa #SQLPASS […]