Quantcast
Viewing latest article 7
Browse Latest Browse All 10

Monitor Disk Space using Powershell

All administrators face this issue, a disk is running low on disk space but you don’t log on to the server that much so you don’t notify it.

That’s why I created this Powershell Script, just insert the name of you’re server in the .txt file and schedule the Powershell script as much as you would like it to run.

There are 2 options :
– Threshold mail (This wil only send an email when the specified Threshold is met
– Full mail (Every time the script runs it will mail a list of you’re servers and the disk capacity)

Function name : Get-FreeSpace

Parameters :
minimumThreshold : The value in % on which a disk will marked as almost full
HIGHPRIO :  The value in % when a send mail will be marked as High Priority
ServerList : Path to the server list txt file
Logging : Boolean value to log to screen
Maillog : Boolean value to log to mail
Mailto : Mandatory when Maillog is used (Recipient of report)
From : Mandatory when Maillog is used (Send of report)
Subject : Mandatory when Maillog is used (Mail subject)
Smtpserver : Mandatory when Maillog is used
CompleteLog : A full report of all server with diskspace will be generated and included
Bold = Mandatory

The script

Function Get-FreeSpace {
Param(
[Parameter(Mandatory=$true, HelpMessage=”Threshold Percentage”)][int] $minimumThreshold,
[Parameter(Mandatory=$true, HelpMessage=”Mark Mail as High Priority Percentage”)][int] $HIGHPRIO,
[Parameter(Mandatory=$true, HelpMessage=”Text File with Server List”)][string] $ServerList,
[Parameter(Mandatory=$false, HelpMessage=”Log to screen?”)][boolean] $Logging,
[Parameter(Mandatory=$false, HelpMessage=”Mail Log?”)][boolean] $MailLog,
[Parameter(Mandatory=$false, HelpMessage=”Mail recipient”)][string] $Mailto,
[Parameter(Mandatory=$false, HelpMessage=”Mail sender”)][string] $From,
[Parameter(Mandatory=$false, HelpMessage=”Mail Subject”)][string] $Subject,
[Parameter(Mandatory=$false, HelpMessage=”SMTP Server”)][string] $Smtpserver,
[Parameter(Mandatory=$false, HelpMessage=”List all CSV’s”)][boolean] $CompleteLog
)

##### [Parameter(Mandatory=$true, HelpMessage=”Server to monitor”)][array] $Servers, ##########
$datetime = Get-Date -Format “yyyyMMddHHmmss”;

# Get server list
$servers = Get-Content $ServerList
$HighPrioFlag = “normal”
$SendMail = $false

if ($MailLog)
  {
  if ($Mailto -eq “”) { $quit = $true }
  if ($From -eq “”) { $quit = $true }
  if ($Subject -eq “”) { $quit = $true }
  if ($Smtpserver -eq “”) { $quit = $true }

  if ($quit)
    {
    write-host -f red “When using MailLog ‘Mailto, From, Subject and Smtpserver’ are required”
  return
    }
}
 
# Add headers to log file
#Add-Content “$Env:USERPROFILE\server disks $datetime.txt” “server,deviceID,size,freespace,percentFree”;

$objs = @()
$FullTable = “<table><tr><td><b>Server</td><td><b>DeviceID</td><td><b>SizeGB</td><td><b>FreespaceGB</td><td><b>PercentFree</td></tr>”
$ThresholdTable = “<table><tr><td><b>Server</td><td><b>DeviceID</td><td><b>SizeGB</td><td><b>FreespaceGB</td><td><b>PercentFree</td></tr>”
 
foreach($server in $servers)
{
 # Get fixed drive info
 $disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter “DriveType = 3″;
 
 foreach($disk in $disks)
 {
  
  $obj = New-Object PSObject
  
  $deviceID = $disk.DeviceID;
  [float]$size = $disk.Size;
  [float]$freespace = $disk.FreeSpace;

  $obj | add-member -membertype noteproperty -name Server -value $server -force
  $obj | add-member -membertype noteproperty -name Size -value $size -force
  $obj | add-member -membertype noteproperty -name DeviceID -value $DeviceID -force

  $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
  $obj | add-member -membertype noteproperty -name PercentFree -value $percentFree -force

  $sizeGB = [Math]::Round($size / 1073741824, 2);
  $obj | add-member -membertype noteproperty -name sizeGB -value $sizeGB -force

  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
  $obj | add-member -membertype noteproperty -name FreespaceGB -value $freeSpaceGB -force

  if ($obj.PercentFree -le $HIGHPRIO)
   {
   $HighPrioFlag = “high”
   }
  
  $objs+=$obj
  
  if ($PercentFree -le $minimumThreshold)
   {
   $SendMail = $true
   $color = “#FF0000″
   $ThresholdTable += “<tr><td>” + $obj.Server + “</td><td>” + $obj.DeviceID + “</td><td>” + $obj.SizeGB + “</td><td>” + $obj.FreespaceGB + “</td><td><b><font color=’$color’>” + $obj.PercentFree + “</font></b></td></tr>”
   }
  else
   {
   $color = “#00FF00″
   }
  $FullTable += “<tr><td>” + $obj.Server + “</td><td>” + $obj.DeviceID + “</td><td>” + $obj.SizeGB + “</td><td>” + $obj.FreespaceGB + “</td><td><b><font color=’$color’>” + $obj.PercentFree + “</font></b></td></tr>”
  

 }

}

$FullTable += “</table>”
$ThresholdTable += “</table>”
$objs = $objs | ft -auto Server,DeviceID,SizeGB,FreespaceGB,PercentFree

if ($Logging)
 {
 $objs
 }

if ($SendMail -OR $CompleteLog) {
if ($MailLog)
 {
 $body = “<b>Threshold Report:</b><br>” +$ThresholdTable
 if ($CompleteLog)
 {
 $body += “<p><b>Full Report:</b></p>” + $FullTable
 }
 #send-mailmessage -BodyAsHtml -body $FullTable -subject $Subject -to $mailto -from $From -smtpserver $Smtpserver -Priority $HighPrioFlag

 $subject = $subject + ” less than ” + $minimumThreshold + “%”
 send-mailmessage -BodyAsHtml -body $body -subject $Subject -to $mailto -from $From -smtpserver $Smtpserver -Priority $HighPrioFlag
 }
}

} #End Function Get-FreeSpace

 Download the script :

Save the script with a name (.ps1) you like on a location you like for example “d:\freespacefunction.ps1″, to use the function use :

Start you’re Powershell window
import-module “d:\freespacefunction.ps1″
Get-Freespace and all the parameters

Example of ServerList
 servers

Usage:
–  Get-Freespace  -minumumThreshold 8 -Highprio 3 -Serverlist “c:\servers.txt” -Logging $true
This will output a list to screen

–  Get-Freespace  -minumumThreshold 8 -Highprio 3 -Serverlist “c:\servers.txt” -Maillog $true -mailto recipient@domain.com -from sender@domain.com -subject “Free disk space report” -smtserver smtp.domain.com
This will send the report of only disks less than 8% freespace to recipient@domain.com, if no disks are less than 8% no mail will be send

–  Get-Freespace  -minumumThreshold 8 -Highprio 3 -Serverlist “c:\servers.txt” -Maillog $true -mailto recipient@domain.com -from sender@domain.com -subject “Free disk space report” -smtserver smtp.domain.com -CompleteLog $true 
This will send the report of all disks to recipient@domain.com every time the script is scheduled

How to schedule a Powershell script:
See my Post :
How to schedule a Powershell script using Scheduled tasks in Windows Server 2008 R2

 


Viewing latest article 7
Browse Latest Browse All 10

Trending Articles