Get Drive Space Including Mount Points

Today’s post is a quick one that came out of a conversation on Twitter. To make a long story short, somebody was having trouble with mount points filling up because they were not being caught by their current monitoring script. I offered to look up how my monitoring was doing this and post it here.

After some digging through various VBScript files I was able to confirm that I use a WMI query of Win32_Volume. The query I have is in a highly custom script that does not lend itself to being understandable if taken out of context so I went ahead and converted the logic to PowerShell, then messed with it a bit to come up with something human readable. Here is that script:

Update: Nicholas Cain (Blog|Twitter) suggested using 1GB to get results in gigabytes instead of using /1024/1024/1024. I was curious about this trick and after a little toying around, was able to adjust the script to allow the units of measure to be specified at the top. So go ahead and give it a try.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cls
[string] $serverName = 'localhost'
[string] $unitOfMeasure = 'GB'  #Use an empty string for bytes or KB, MB, GB, TB, PB etc.

$vols = Get-WmiObject -computername $serverName -query "select Name, DriveType, FileSystem, FreeSpace, Capacity, Label from Win32_Volume where DriveType = 2 or DriveType = 3"

foreach($vol in $vols)
{
    [string] $driveType = switch ($vol.DriveType)
    {
        0 {'Unknown'}
        1 {'No Root Directory'}
        2 {'Removable Disk'}
        3 {'Local Disk'}
        4 {'Network Drive'}
        5 {'Compact Disk'}
        6 {'RAM Disk'}
        default {'unknown'}
    }
    [string] $drive = "Drive: {0}, {1}, {2}, {3}" -f $vol.name, $driveType, $vol.FileSystem, $vol.Label
    [string] $capacity = "Capacity: {0}{1}" -f [System.Math]::Round(($vol.capacity / $('1' + $unitOfMeasure)), 0), $unitOfMeasure
    [string] $freeSpace = "Free Space: {0}{1}" -f [System.Math]::Round(($vol.FreeSpace / $('1' + $unitOfMeasure)), 0), $unitOfMeasure
    Write-Output $drive
    Write-Output $capacity
    Write-Output $freeSpace
    Write-Output ""
}

So there is the code as promised. The next logical step would be to add a call inside the loop to insert this information into a table for trending and alerting.

Please do not run this on an important machine before reviewing it thuroughly. I offer no warranty beyond a sympathetic ear if this script breaks something.

This entry was posted in PowerShell and tagged , , . Bookmark the permalink.

4 Responses to Get Drive Space Including Mount Points

  1. Pingback: Tweets that mention Get Drive Space Including Mount Points | Adventures in SQL -- Topsy.com

  2. Nic Cain says:

    One thing I’ve found recently is making the math a little easier.

    Where you have $vol.FreeSpace / 1024 / 1024 / 1024 you can actually replace with $vol.FreeSpace / 1GB

    One of those little powershell nuggets that makes life so much easier.

  3. David Levy says:

    I like that alot! I did it the way I did to make it easier to move from GB to MB to KB but I am assuming you could divide by 1MB or 1KB just the same.

  4. Pingback: Quick Blog: PowerShell Disk and MountPoint Check – SQLvariations: SQL Server, a little PowerShell, maybe some Hyper-V

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>