Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

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:

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
cls
[string] $serverName = 'localhost'

$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}GB" -f [System.Math]::Round(($vol.capacity / 1024 / 1024 / 1024), 0)
    [string] $freeSpace = "Free Space: {0}GB" -f [System.Math]::Round(($vol.FreeSpace / 1024 / 1024 / 1024), 0)
    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.

Comments

Posted by Anonymous on 24 November 2010

Pingback from  [Video] Math Trick: Why Tables Beyond 5x5 Are Unnecessary | The Memorization Blog

Posted by Anonymous on 13 July 2011

Pingback from  Get Drive Space Including Mount Points | SQL Server | Syngu

Leave a Comment

Please register or log in to leave a comment.