Powershell Script: Send an email to alert about disk space

You may want to use this script to keep track of your rapid growing drives.  In this scenario, we have two computers that I want to keep track due to the rapid increase in size of SQL transaction log files.

Disk size is calculated in bytes.  For example:

1 kilobyte = 1,024 bytes

1 megabyte = 1,024 kilobytes

1 gigabyte = 1,024 megabytes

85 gigabytes = 91,268,055,040 bytes

Function Mailer ($emailTo,$computerName,$subject,$message)
{
$emailFrom = "testalert@yahoo.com"
$smtpserver="smtp.test.com"
$smtp=new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $message)
}

$SQLServers = "COMPUTERNAME1", "COMPUTERNAME2"
$alertDiskSpace = 91268055040
foreach ($computerName in $SQLServers){
	$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $computerName -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
	if ($disk.FreeSpace -lt $alertDiskSpace)
	{
		$subjectline = $computerName + ": C drive current free space " + ([math]::truncate(($disk.freeSpace / 1GB))) + " GB"
		$messageBody = "Please check SQL replications `n" +
			"Server name: $computerName `n" +
			"Alert Disk Trigger: " + ([math]::truncate($alertDiskSpace / 1GB)) + " GB `n" +
			"C Drive Total Space: " + ([math]::truncate($disk.size / 1GB)) + " GB `n" +
			"C Drive Free Space: " + ([math]::truncate($disk.freeSpace / 1GB)) + " GB"
		Mailer -emailTo "admin@ephams.com" -computerName $computerName -subject $subjectline -message $messageBody
	}
}

Comments are closed