Powershell

Monitor Home IP Address Using Powershell with email Notify

Many of you may need to monitor your home IP address due to services you are running or simply to know your WAN IP address in order to RDP into a workstation you have back home.  The problem of course as we all know is most ISPs use DHCP in the residential market which presents a challenge keeping up with your IP address as these tend to change somewhat often.

In this post we want to consider a solution without using 3rd party utilities and only by using builtin Powershell functionality in Windows 7 as well as the Task Scheduler to run a process to check your IP address and then notify via email address or text message as your IP address changes.

In the Microsoft Script Center there is a really great script to be found here which using powershell allows you to grab your WAN IP address and then store this in a variable.  Check it out here:  https://gallery.technet.microsoft.com/scriptcenter/Get-StaticPublic-IP-09d7695c .  You can use this script to find your WAN IP and then take the information in variable and use it to email you when the ip address changes.  Here is how we can do that.

First

We pipe the output from the WAN IP address powershell script to a text file.

Second

We use another powershell script to get the contents of the piped out text file and comparing that with your current IP address that you know.  If the IP address is the same, it won’t send the alert.  However, if the IP addresses do not match, a notification email is send to recipient(s) of your choice.

Powershell script to read text file and email:

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "[email protected]"
$Password = "password"
$ip = get-content c:\test.txt

$to = "[email protected]"
$cc = "@vtext.com"
$subject = "New IP Address Detected"
$body = "New IP Address '$ip'"
$attachment = "C:\test.txt"

$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.cc.add($cc)
$message.from = $username
$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);

if ($ip -ne "X.X.X.X") {

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

}

In the above script, you replace of course the email address credentials as well as receipients.  You can specify either ATT or Verizon email to text addresses also if you want to be alerted via text message as well.  Also, replace the “X.X.X.X” with your current IP address.  The script will run and if the results match what you think your IP is, it will not send an email.  However, if they are different, you will receive the notification messages.

UPDATE – Take a look at this post for a little bit more logic added to the above script to minimize false positives.

Batch File

In order to run this script, we call both with a very simple batch file (contents below) which runs both powershell scripts and we can use to schedule using the built in Windows Task Scheduler.  You can name the script anything you want, i.e. ipmail.bat.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command .\Static-Ip.ps1" > c:\test.txt
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command .\sendmail.ps1"

Schedule

Simply schedule the above batch file using Task Scheduler in Windows to run as often as you would like to check your IP address, whether every 1 hour or even more often.

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 has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon 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.

Related Articles

One Comment

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.