Disk Storage alert email - Powershell

Viewed 795

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

enter image description here

Can't seem to find or understand how it's generating additional lines of text. Any insight would be greatly appreciated!

1 Answers

Try using a more simplified code like the one below. I use this with a scheduled task on the machines I need it on. It only sends on email if disk space gets below threshold. You could modify it to work with the other drives you have. It's not really clear how you are getting multiple line output but modifying it would help resolve your problem and simplify your code.

$minGbThreshold = 10;
$computers = $env:COMPUTERNAME;
$smtpAddress = "smtp.yourdomain.com";
$toAddress = "anyone@gmail.com";
$fromAddress = "someone@gmail.com";
foreach($computer in $computers)
{    
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3";
    $computer = $computer.toupper();
    $deviceID = $disk.DeviceID;
    foreach($disk in $disks)
    {
        $freeSpaceGB = [Math]::Round([float]$disk.FreeSpace / 1073741824, 2);
        if($freeSpaceGB -lt $minGbThreshold)
        {
            $smtp = New-Object Net.Mail.SmtpClient($smtpAddress)
            $msg = New-Object Net.Mail.MailMessage
            $msg.To.Add($toAddress)
            $msg.From = $fromAddress
            $msg.Subject = “Diskspace below threshold ” + $computer + "\" + $disk.DeviceId
            $msg.Body = $computer + "\" + $disk.DeviceId + " " + $freeSpaceGB + "GB Remaining";
            $smtp.Send($msg)
        }
    }
}
Related