Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How can I send an email when a function returns false?

$
0
0

Im trying to make this code work, but i still receive an email when the function is equal to false. Can anyone assist me in finding the issue? What the program does is look for servers that has a set limit (in GB) on specific drives.

#This Program goes through all the servers and informs any level below the limit
$ErrorActionPreference = "SilentlyContinue";
$scriptpath = $MyInvocation.MyCommand.Definition 
$dir = Split-Path $scriptpath 


#=================Function============

Function isLow($server, $drive, $limit)
{	
   
	$disk = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3 and DeviceID = '$drive'";
	[float]$size = $disk.Capacity;
	[float]$freespace = $disk.FreeSpace; 
	$sizeGB = [Math]::Round($size / 1073741824, 2); 
	$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
		if($freeSpaceGB -lt $limit){
			return $true;
		}
		else{
			return $false;
			}
}

#================Servers==============
#------------------###server1--------------
$server = "###server1";
$drive = "C:";
$lim = 25;

if(isLow $server $drive $lim)
{
	$Alert += $server + "  ||   " + $drive + "   is low<br>"
}

$server = "###server1";
$drive = "D:";
$lim = 35;
if(isLow $server $drive $lim)
{
	$Alert += $server + "  ||   " + $drive + "   is low<br>"
}


#-----------------###(more servers ect.)--------------

#================EMAIL===============

$smtpServer = "192.168.x.x" 
$ReportSender = "sender@email.com"  
$users = "user@email.com"
$MailSubject = "ALERT!!! Low DiskSpace"


foreach($user in $users){
if($true){
	Write-Host "Sending Email notification to $user"
	$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
		$msg = New-Object Net.Mail.MailMessage
		$msg.To.Add($user)
        $msg.From = $ReportSender
		$msg.Subject = $MailSubject
        $msg.IsBodyHTML = $True
        $msg.Body = $Alert
		$smtp.Send($msg)
		}
else($false){
	Write-Host "No one is pass the limit"
}
}


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>