Pages

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 {
<#
.SYNOPSIS
   Returns report on mount points for specific server
.DESCRIPTION
 Reads mount points and maps to volumes on a specific server
.PARAMETER ServerName
   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

.EXAMPLE
   Get-MountPOintInfo -ServerName "FilePrintServer" -Detailed
#>
 [CmdLetBinding()]
 param(
  [parameter(Mandatory=$true)][string]$ServerName,
  [Parameter()][switch]$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) {
  $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
    $Record | add-Member -memberType noteProperty -name Name -Value $V.name
    if ($detailed.ispresent) {
     $DestFolder = "\\"+$ServerName + "\"+ $v.name.Substring(0,$v.name.length-1 ).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 $V.name
    $Summary += $Record
   }
  }
 }
 return $summary
}

No comments:

Post a Comment