03 March, 2010

Powershell - Sending IP address in email via GMAIL

I wanted to be able to Remote Desktop to my Home PC from work...

So, I had to enable RDP on the PC and set up port forwarding on my wireless router for  port 3389 (the machine I'm connecting to is Hard wired) - Port forwarding is easy enough, but the steps are specific to your brand of router (Google is your friend!). 

The last step was to open RDP on my work computer and connect to my home computer. To do this I needed the EXTERNAL IP address on my router. Your EXTERNAL IP information is not found on / in the physical machine you want to connect to if you use a router. It is presented to the outside facing portion of your network connection so you have to ask the outside world what IP address they see you using...

And the EXTERNAL IP is subject to change at various points in time depending on the dynamic IP lease / renew rate your ISP dictates... Unless you have a Static IP address. That is not to say that your dynamic IP address will change, but it can.

Rather than spending the money on a Static IP address, I decided to find a way to make my EXTERNAL IP address available to me while I was away (somehow). Enter Powershell...

(Note: I run Win 7 64 bit, so I use PS v2.0)

After some digging around / trial and error. I found a method that will ask for my EXTERNAL IP address (at 'http://checkip.dyndns.com:8245/') and send that information via e-mail (I use GMAIL) from my machine to my GMAIL account.

The initial return from grabbing the results of that websites information are brought in to Powershell as raw text (including the HTML tags):    
<html><head><title>Current IP Check</title></head><body>Current IP Address: xx.xx.xx.xx</body></html>




There are (of course) plenty of ways to remove the extraneous data and there are also ways to embed the HTML into the email...


I used this '-replace "[^\d\.]"' to strip out the tags and the letters (it replaces anything that is not a 'number' or a 'period', with a ~null~ value) - its function is also commented in the script:
    
This script is seeminly specific to GMAIL port and SSL requirements, but it may work for other systems as well.

I hope that this information can help someone else - in part or in whole:


##############  START ###############
$wc=New-Object net.webclient
$Results = $wc.downloadstring("http://checkip.dyndns.com:8245/") -replace "[^\d\.]"

#############################################################################################

####                       NOTES about " -replace "[^\d\.]"                              ####
####                                                                                     ####
#### "-Replace" replace "^" all Characters excluding "\d" numbers or "\." decimal points ####
####  ~ it literally only retains numbers and decimal points ~                           ####
####                                                                                     ####
#############################################################################################

$Username = "
XXXXXXXXXX" # account username goes inside the parenthesis
$PW = "xxxxxxxxxx" # account password goes inside the parenthesis"

$EmailFrom = "XXXXXXXX@gmail.com"
#FROM 'GMAIL' address goes inside the quote marks
$EmailTo = "xxxxxxxx@gmail.com"
# RECIPIENT address goes inside the quote marks
$Subject = "$Results - Home IP Address"

$Body = "$Results - Home IP Address"


$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $PW);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

##############  END ###############