Powershell

Powershell if equals and not null

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!

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.