Thursday, January 2, 2014

Get Mount Point Info for Specific Server

There comes a time when I want to scan a server for all the mount points on a server. This function can be added to a script to generate two slightly different reports. First, stand-alone it will quickly return a report of all the mount points on a server. Second, if you run it with the '-detailed' switch, it will return the capacity, used and free space on each. The Detailed process takes considerably longer as it reads each file in that mount point.

Name          : D:\Databases\Mailbox-DB03\
FileSize      : 178617132724
CapacityinGB  : 931.36
FreeSpaceinGB : 764.85
UsedSpaceinGB : 166.51
FreePercent   : 82

The script will return an object containing the info, so the values can be used again.

$ServerMPInfo = get-MountPointInfo -servername "MailboxServer1" -detailed
$LowSpace = $ServerMPInfo | ?{$_.FreePercent -lt 10}

function Get-MountPointInfo {
   Returns report on mount points for specific server
 Reads mount points and maps to volumes on a specific server
   Name for the server to run this report against. 
.PARAMETER Detailed (Optional)
   Add file size on each mount point. If not selected, it will only return mounted volume names

   Get-MountPOintInfo -ServerName "FilePrintServer" -Detailed

 $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 ,Capacity

 foreach ($MP in $Mountpoints) {
  $ = $"\\","\")
  foreach ($v in $Volumes) {
   $vshort = $,$ )
   $vshort = """$vshort""" #Make it look like format in $MP (line 11).
   if ($$vshort)) { #only queries mountpoints that exist as drive volumes no system
    $Record = new-Object -typename System.Object
    $Record | add-Member -memberType noteProperty -name Name -Value $
    if ($detailed.ispresent) {
     $DestFolder = "\\"+$ServerName + "\"+ $,$ ).Replace(":","$")
     #$destFolder #troubleshooting string to verify building dest folder correctly.
     $colItems = (Get-ChildItem $destfolder -Recurse | 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)
     $CapacityinGB = [Math]::Round(($v.Capacity/1073741824),2)
     $FreeSpaceinGB = [Math]::Round(($v.freespace/1073741824),2)
     $usedSpace = $v.Capacity - $v.freespace
     $usedSpace = [Math]::Round(($usedSpace / 1073741824),2)
     $freePercent = ($v.freespace/$v.Capacity)*100
     $freePercent = [Math]::Round($freePercent,0) 
     $Record | add-Member -memberType noteProperty -name FileSize -Value $fsize
     #This will show space in bytes: $Record | add-Member -memberType noteProperty -name FreeSpace -Value $v.freespace
     #This will show space in bytes: $Record | add-Member -memberType noteProperty -name Capacity -Value $v.Capacity
     $Record | add-Member -memberType noteProperty -name CapacityinGB -Value $CapacityinGB
     $Record | add-Member -memberType noteProperty -name FreeSpaceinGB -Value $FreeSpaceinGB
     $Record | add-Member -memberType noteProperty -name UsedSpaceinGB -Value $usedSpace
     $Record | add-Member -memberType noteProperty -name FreePercent -Value $freePercent
    $Record | add-Member -memberType noteProperty -name Name -Value $
    $Summary += $Record
 return $summary

No comments:

Post a Comment