Powershell Script - Send Email Only When Drive Space Is Below MinThreshold

1

I'm using Windows 10 Enterprise 1709 and PowerShell version 5.0. I have this PowerShell script below that I found online. It works but I don't understand what a line in code is doing so I can decided how to edit it for what I need to do. Also, it sends an email even though the drive is not below minimum threshold. I would like to understand this line of the code and fix it to only send email alert when it reaches below the minimum threshold. $freeSpaceGB = [Math]::Round([float]$disk.FreeSpace / 1073741824);and this line $computer = $computer.toupper();

The entire code is below:

$minGbThreshold = 50GB

$computers = $env:COMPUTERNAME
$smtpAddress = "smtp.anydomain.com"
$toAddress = "anyone@anywhere.com"
$fromAddress = "desktop-admin@anywhere.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);
        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)
        }
    }
}

webby68

Posted 2019-11-26T18:53:23.187

Reputation: 151

I now understand the math line and what it is doing. I still need to understand why I still get an email even though the disk space isn't below the minimum – webby68 – 2019-11-26T19:09:40.767

What is the exact value of freeSpaceGB when the email is sent. – Ramhound – 2019-11-26T19:10:58.097

@Ramhound I get the exact value of free space on the drive. – webby68 – 2019-11-26T19:18:17.377

I want to know the exact decimal value. Due to the rounding function 50.44 GB would round down to 50 GB. If you want an explanation on the behavior you must provide specific examples, otherwise, I will leave my answer as-is which only explains the parts of the script you your question asked about. – Ramhound – 2019-11-26T19:23:40.400

@Ramhound there is no output in the ide. It sends an email and this is what it shows in the email. I'll have to get that information on the screen. ABC-L-ABCDEF\C: 57GB Remaining – webby68 – 2019-11-26T19:28:59.537

1@PimpJuiceIT Yes, used it but this is only for 12 machines and I'm trying to learn more PS scripting. Thanks for comment. – webby68 – 2019-12-17T14:14:29.697

Answers

2

$freeSpaceGB = [Math]::Round([float]$disk.FreeSpace / 1073741824);

Determines the amount of free storage space the HDD has in Gigabytes (GB) to the nearest GB.

1,073,741,824 bytes is 1.073741824 Gigabytes which is the amount of bytes that Windows uses to indicate a single Gigabyte (GB).

2^30 = 1,073,741,824

The gibibyte is a multiple of the unit byte for digital information. The binary prefix gibi means 230, therefore one gibibyte is equal to 1073741824 bytes.

Source: Gibibyte

$computer = $computer.toupper();

Simply changes to the case of the name of the computer to all uppercase.

I still need to understand why I still get an email even though the disk space isn't below the minimum

$minGbThreshold = 50GB
$freeSpaceGB = [Math]::Round([float]$disk.FreeSpace / 1073741824);

The integer value of 57 is indeed less than the alphanumeric value of the string 50GB.

Explanation:

61,717,860,352 / 1,073,741,824 = 57.4792

57.4792 would round down to a integer value of 57

The original snippet actually indicated the correct value for $minGbThreshold.

$minGbThreshold = 25

Source: Automated Disk Space Alerts For Windows Server

Ramhound

Posted 2019-11-26T18:53:23.187

Reputation: 28 517