Script originally created by GhillieHammer
I've added additional features to work for me.
# $drives = @("D","E","F");
$drives = $null
# The minimum disk size to check for raising the warning
$minSize = 20GB
$MathminSize = [math]::round($minSize/1GB,2)
$minString = ($MathminSize).ToString()
$minString = $minString + "GB"
$email_to_addressArray = @("");
# $email_to_addressArray = @("toSingle@company.com")
#Set a couple needed variables
$SendIt = $Null
$ThisHost = $env:computername
$IPAddy = Invoke-RestMethod -Uri ('http://ipinfo.io/'+(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content) | Select ip
#Check for $Drives query mode and make an array of them
if ($drives -eq $null -Or $drives -lt 1) {
$localVolumes = Get-WmiObject win32_volume;
$drives = @();
foreach ($vol in $localVolumes) {
if ($vol.DriveType -eq 3 -And $vol.DriveLetter -ne $null ) {
$drives += $vol.DriveLetter[0];
}
}
}
# Enumerate through the array of drives
# check to see if any are below minimum
# if so, set a flag saying as much and then
# add them and their information to the array we'll be adding to the email
foreach ($d in $drives) {
$disk = Get-PSDrive $d;
$MathDiskFree = [math]::round($disk.Free/1GB,2)
$MathDiskUsed = [math]::round($disk.Used/1GB,2)
$MathDisktotal = [math]::round($MathDiskFree + $MathDiskUsed)
$MathDiskPerc = ($MathDiskFree / $MathDiskUsed).tostring("P")
if ($disk.Free -lt $minSize) {
$SendIt = 1
$space += ("Free space on drive " + $d + " = " + $MathDiskFree + "GB. This is equal to only " + $MathDiskPerc + " of the " + $MathDisktotal + "GB total space available on this drive.<br>")
}
}
# Check the flag to see if it's set, meaning there's at least one drive below minimum free space, and if so, fire off the email(s)
If ($SendIt -eq 1) {
# Enumerate through the array of email addresses and fire off a formatted email to each
foreach ($toAddress in $email_to_addressArray) {
$User = "diskspace@"
$File = (Get-Content C:\Temp\pw.txt | ConvertTo-SecureString)
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, $File
$To = $toAddress
$from = ""
$EmailSubject = "WARNING: one or more disk on $ThisHost low on space"
$smtp = "auth.smtp.1and1.co.uk"
$DefaultMessage="
<p>Dear colleague,</p>
<p>There is a hard drive space issue on $ThisHost IPv4: $IPAddy </p>
<p>$space<br></p>
<p>This message only fires off if one or more disks have less than $minString GB of free space left.</p>
<p>Sincerely,<br>
Robot Monitor.<br><br>
</p>"
$MailMessage = @{
To = $To
From = $from
# BCC = $Bcc
Subject = $EmailSubject
Body = $DefaultMessage
priority = "High"
Smtpserver = $smtp
Credential = $MyCredential
ErrorAction = "SilentlyContinue"
}
Send-MailMessage @MailMessage -bodyashtml
}
}
The script works perfectly but can't seem to get it stop generating the following information
Can't seem to find or understand how it's generating additional lines of text. Any insight would be greatly appreciated!
