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)
}
}
}
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 to50 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