Powershell

Powershell if equals and not null

A while back, I wrote a blog post about using Powershell to email notify you if your home IP address changes if you are dynamically assigned an IP from your ISP.  However, I wanted to write a quick post about an update I have been meaning to do to tweak the script slightly to use just a touch more logic to keep from receiving false alarm emails from the process.

Background

The powershell script works on a process that runs via scheduled task to hit a service to pull the current WAN ip.  Based on a text file set to the current known IP, a comparison is made and if there is a difference, you get an email which contains the current IP address.

The problem I started having with the script is that if the service to determine the public WAN ip didn’t return the value in an acceptable time period or something happened network wise to prevent the population of the IP text file with an IP address, the file would be modified and left empty.  As a result, emails would come through attaching the text file with the “new” IP address of “blank” even though the IP address had in fact not changed.

I needed some logic in the script to handle if the file was empty.  So the slight one liner change to allow powershell if equals and not null logic:

if (($ip -ne "xxx.xxx.xxx.xxx") -And ($ip -ne $null)){

$smtp.send($message)
write-host "Mail Sent"

}

Again read the full post for the full script to email notify on IP address change, but adding the logic has allowed for proper alerting without the null empty email alerts if the service failed to obtain the proper public IP for some reason.

Future

Look for a future write up of some other logic that I will be adding to the script.  Stay tuned!

Back to top button