Description

This script reads mount point locations and free space thresholds from a text file, checks the % free space, and sends an email alert of free space is below the threshold.

This is one of my first powershell scripts, please excuse amount of comments and notes.

Script

PowerShell
Edit|Remove
#Author: Nicholas Sinclair
#Date:   Sept 3, 2010
#
#Checks freespace of Windows mount point if mount point UNC exists
#Change URL for input list as necessary

function email{ #function to send email
	$emailFrom = "<Enter From address here>"
	$emailTo = "<Enter To address here>"
	$subject = "Drive Space Below $Threshold"
	$body = "The Drive Space for $Server - $Token is Under $Threshold Percent ($Data % Free)"
	$smtpServer = "<Enter SMTP Server here>"
	$smtp = new-object Net.Mail.SmtpClient($smtpServer)
	$smtp.Send($emailFrom, $emailTo, $subject, $body)
}

function manipulate(){ #function checks freespace counter, checks against threshold, calls email of lower than threshold
	$Counter = Get-Counter -ComputerName $Server -counter "\LogicalDisk($Token)\% Free Space" #freespace counter
	$Data = [decimal]$Counter.Readings.TrimStart("\\$Server\\logicaldisk($Token)\% free space :") #trims all information but actual freespace from counter output
	$Data = "{0:N2}" -f $Data
	if ($Threshold -ge $Data){
		email #sends email if freespace is lower than threshold specified
	} 
}

$readline = gc "<Enter location of server input list here>" #reads from unc list
foreach ($Line in $readline){ #loop to read each line and set variables 
	$Split = $Line.Split("`t") #splits assigns unc and threshold from file to array
	$Server = $Split[0].substring(2,9) #extracts server name from unc
	$Token = $Split[0].substring(12) #extracts drive inforamtion from unc
	$Token = $Token.Replace("$", ":") #fixes drive formatting 
	$Token = $Token.ToLower() #sets drive to lowercase !script breaks if not lowercase!
	[decimal]$Threshold = $Split[1] #assigns threshold information
	if(test-path $Split[0]){ #checks to see if unc exists, only proceeds with script if it does.
		manipulate #calls manipulation function
	}
}

#Notes:
#$Split[0] - unc path
#$Split[1] - threshold
#
#List UNC Path, tab, %freespace threshold in the input file
#ex. \\UNC_PATH	100
#List each UNC path on a separate line in input file
#Items in chevrons are dependant on your specific emails, server names, or file locations