PowerShelling for kids

So I have been doing some  powershelling lately, found this one line that is helping me out.  It is from Eric Woodford and the focus of the line is to gather the mounted disks for a windows server.  Basically it is like this, I have an Exchange 2007 server using a CCR cluster and the server uses DAS so the LUNS carved out for this are 15 disks for logs and 15 disks for databases.  Now the disks are volume mounted disks so they don’t show up as E drives or D drives.  But they do show up in the disk manager on the server.  So in order to monitor space on all 30 mounted volumes I could either use the disk manager or find a way to get all the disk information and export it out to view and analyze in excel.  This script is perfect.  It gathers all mounted volumes and then reports them onto a html file which I open in excel.  Now the powershell that gathers this info is called GetMountPointInfo.  It returns a lot of great info like freespace and Percentage Free, but the freespace data numbers it gives is in BYTES and not KB or GB.  So since my boss needs to know how many GB we use and are available then I had to do some excel formula’s to convert the BYTES into GB’s.  Not a biggie but it works.  Thanks to Eric for posting this like 3 years ago.

#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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s