Query Size and Free Space of Windows Volume Mount Points

I have been tasked with writting a script to analyze our servers on a daily basis. I found this most excellent server inventory script that pulls virtually every conceivable value from a server.

Now, on our Exchange cluster, we've used Volume Mount Points for the resources. This means that a 9gb drive with eight 330gb folders (or mount points) to SAN storage. If I simply pull the drive freespace, I get ~6gb (local hard drive), even though the mount points each show over 100gb free.

So.. this script. It queries the server ($ServerName) and looks up all the mount points, then queries the volumes and returns the current size, free space, and % free for each labeled mount point.

Note (Nov 10, 2008): I cleaned up a warning in the script that popped up a false error when the folder was empty. In addition, I realized the data is more useful to me in a record format, so I have the data returned in an array instead of simple string (CSV) format. Powershell displays the data for one server like this:

Name FileSize FreeSpace PercFree
I:\SG1\ 229641617408 128873562112 36 %
I:\Utility\ 0 347809751040 100 %
I:\SMTPMTA\ 0 53606744064 100 %

 
#Get-MountPointInfo.PS1 Script
#Eric Woodford
#Scripts@ericwoodford.com
#Nov 11, 2008
#Discover and detail volume mount points on a specified Windows server.
#

function Get-MountPointInfo($ServerName) {
        $Summary = @()
       
        $objFSO = New-Object -com Scripting.FileSystemObject
        $MountPoints = gwmi -class "win32_mountpoint" -namespace "root\cimv2" -computername $ServerName
        $Volumes = gwmi -class "win32_volume" -namespace "root/cimv2" -ComputerName $ServerName| select name, freespace


        foreach ($MP in $Mountpoints) {
                $MP.directory = $MP.directory.replace("\\","\")        
                foreach ($v in $Volumes) {
                        $vshort = $v.name.Substring(0,$v.name.length-1 )
                        $vshort = """$vshort""" #Make it look like format in $MP (line 11).
                        if ($mp.directory.contains($vshort)) { #only queries mountpoints that exist as drive volumes no system
                                $Record = new-Object -typename System.Object
                                $DestFolder = "\\"+$ServerName + "\"+ $v.name.Substring(0,$v.name.length-1 ).Replace(":","$")
                                #$destFolder #troubleshooting string to verify building dest folder correctly.
                                $colItems = (Get-ChildItem $destfolder |  where{$_.length -ne $null} |Measure-Object -property length -sum)
                                #to clean up errors when folder contains no files.
                                #does not take into account subfolders.
                               
                                if($colItems.sum -eq $null) {
                                        $fsize = 0
                                } else {
                                        $fsize = $colItems.sum
                                }
                               
                                $TotFolderSize = $fsize + $v.freespace
                                $percFree = "{0:P0}" -f ( $v.freespace/$TotFolderSize)
                                $Record | add-Member -memberType noteProperty -name Name -Value $V.name
                                $Record | add-Member -memberType noteProperty -name FileSize -Value $fsize
                                $Record | add-Member -memberType noteProperty -name FreeSpace -Value $v.freespace
                                $Record | add-Member -memberType noteProperty -name PercFree -Value $percFree
                                $Summary += $Record
                        }
                }
        }
        return $Summary
}



$ServerName = "YourServerNameHere"
Get-MountPointInfo($ServerName) | convertto-html -title $ServerName > c:\Report-DriveSpace_for_$ServerName.html