# Get the current IP address of the system

Function getActiveIP

{

$source = "http://ifconfig.me/ip"

$wc = New-Object net.webclient

$ip = $wc .downloadstring ( $source )

return $ip

}

# Change the IP address of the DNS record on dyn.he.net

Function updateDNSIP

{

# How this function explicitly works depends on the dyndns provider's implementation

# In Hurricane Electric's case, it requires an FQDN and a "password" generated

# on the website.

$password = '<password>'

$hostname = '<f.q.d.n>'

$address = "http://dyn.dns.he.net/nic/update?hostname="

$wc = New-Object net.webclient

$wc .Credentials = new-object System.Net.NetworkCredential ( $hostname , $password )

$result = $wc .downloadstring ( $address + $hostname )

return $result

}

# Send an email to let me know if something happened

Function sendEmail ( $address , $subject , $body )

{

$gmailUsr = '<gmail login username>'

$gmailPass = '<gmail login password>'

$gmailSrv = 'smtp.gmail.com'

$smtpc = New-Object net.mail.smtpclient ( $gmailSrv , 587 )

$smtpc .EnableSsl = $true

$smtpc .Credentials = New-Object system.net.networkcredential ( $gmailUsr , $gmailPass )

$smtpc .Send ( $gmailUsr , $address , $subject , $body )

}

# Compare current external IP to stored IP, to see if it's changed.

Function checkIP

{

$oldIP = ( Get-Content C:\ip.txt ) [ 0 ]

write-host $oldIP

$newIP = getActiveIP

write-host $newIP

$retVal = $true

if ( $newIP .contains ( $oldIP ) )

{

# No change in IP. Stop operation here

}

else

{

# Update the stored IP and return false

Set-Content C:\ip.txt $newIP

$retVal = $false

}

return $retVal

}

# Set up the system to allow unsafe headers in web requests

# http://www.leeholmes.com/blog/2007/03/19/converting-c-to-powershell/

$netAssembly = [ Reflection.Assembly ] :: GetAssembly ( [ System.Net.Configuration.SettingsSection ] )

if ( $netAssembly )

{

$bindingFlags = [ Reflection.BindingFlags ] “Static , GetProperty , NonPublic”

$settingsType = $netAssembly .GetType ( “System.Net.Configuration.SettingsSectionInternal” )

$instance = $settingsType .InvokeMember ( “Section” , $bindingFlags , $null , $null , @ ( ) )

if ( $instance )

{

$bindingFlags = “NonPublic” , “Instance”

$useUnsafeHeaderParsingField = $settingsType .GetField ( “useUnsafeHeaderParsing” , $bindingFlags )

if ( $useUnsafeHeaderParsingField )

{

$useUnsafeHeaderParsingField .SetValue ( $instance , $true )

}

}

}

# Check the stored IP

$curIP = checkIP

if ( $curIP -eq $false )

{

# It's changed, operate!

#Attempt to update the DNS address

$resMessage = updateDNSIP

if ( $resMessage .Contains ( "nochg" ) )

{

# Update failed!

$curIP = getActiveIP

sendEmail "<your@email" "DNS Change FAILED" "Message and IP: $resMessage `r `n CurrentIP: $curIP"

[ Diagnostics.EventLog ] ::WriteEntry ( "DDNS Update Script" , "FAILED IP change: $resmessage" , [ Diagnostics.EventLogEntryType ] ::Error )

}

else

{

# Update successful. Carry on.

sendEmail "<your@email>" "DNS Change successful" "New IP: $resMessage"

[ Diagnostics.EventLog ] ::WriteEntry ( "DDNS Update Script" , "Successful IP change: $resmessage" , [ Diagnostics.EventLogEntryType ] ::Notice )

}

}

else

{

write-host "No change in IP"