Skip to content

Instantly share code, notes, and snippets.

@briped
Created May 30, 2014 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briped/e8cff08cd6ee80561945 to your computer and use it in GitHub Desktop.
Save briped/e8cff08cd6ee80561945 to your computer and use it in GitHub Desktop.
Quick PowerShell script to update GratisDNS.dk DDNS. Requires PowerShell v3
Param(
$Username = "user",
$Password = "pass",
$Domainname = "domain",
$Hostname = "host"
)
$UpdateURL = "https://ssl.gratisdns.dk/ddns.phtml?u=$Username&p=$Password&d=$Domainname&h=$Hostname"
$ScriptPath = $script:MyInvocation.MyCommand.Path
$IPCacheFile = $ScriptPath.Substring(0,$ScriptPath.LastIndexOf(".")) + "-CachedIP.txt"
If (!(Test-Path -Path $IPCacheFile)) {
Write-Host "File '$IPCacheFile' does not exist. Creating it and resetting Cached IP."
Write-Output "0.0.0.0" | Out-File -FilePath $IPCacheFile
}
$CachedIP = Get-Content -Path $IPCacheFile -ReadCount 1
Write-Host "Cached IP is: '$CachedIP'."
$ResolvedIP = (Resolve-DnsName -Name $Hostname -Server 8.8.8.8 -ErrorAction SilentlyContinue).IPAddress
Write-Host "Resolved IP is: '$ResolvedIP'."
If ((($ResolvedIP -as [IPAddress]) -as [Bool]) -and ($CachedIP -ne $ResolvedIP)) {
Write-Host "Resolved IP '$ResolvedIP' is valid, but does not match Cached IP '$CachedIP'. Getting the Actual IP."
$ActualIP = (Invoke-WebRequest -Uri "http://checkip.dyndns.com" -ErrorAction SilentlyContinue).Content -replace "[^\d\.]"
Write-Host "Actual IP is: '$ActualIP'."
If ((($ActualIP -as [IPAddress]) -as [Bool])) {
Write-Host "Actual IP '$ActualIP' is valid."
If ($ActualIP -ne $ResolvedIP) {
Write-Host "Actual IP '$ActualIP' does not match Resolved IP '$ResolvedIP'."
$Update = (Invoke-WebRequest -Uri $UpdateURL -ErrorAction SilentlyContinue).Content
If ($Update.Substring(0,2) -ne "OK") {
Write-Host "Update status was not 'OK'. Resetting Cached IP. Request returned: $Update"
Write-Output "0.0.0.0" | Out-File -FilePath $IPCacheFile
}
Else {
Write-Host "Update status was OK. Updating Cached IP."
Write-Output $ActualIP | Out-File -FilePath $IPCacheFile
}
}
Else {
Write-Host "Actual IP '$ActualIP' already matches Resolved IP '$ResolvedIP'. Updating Cached IP."
Write-Output $ActualIP | Out-File -FilePath $IPCacheFile
}
}
}
Write-Host "Script completed."
@bgeskov
Copy link

bgeskov commented Feb 14, 2017

Hello
I saw your code and I found a error.
The code from line 21 will only execute if the $ResolvedIP change and not if the $ActualIP change.
You will have to get the $ActualIP before line 21 and check it within the line 21.
Then it will have no problem again.

I made these change and this should work better:

$CachedIP = Get-Content -Path $IPCacheFile -ReadCount 1
Write-Host "Cached IP is: '$CachedIP'."
$ResolvedIP = (Resolve-DnsName -Name $Hostname -Server 8.8.8.8 -ErrorAction SilentlyContinue).IPAddress
Write-Host "Resolved IP is: '$ResolvedIP'."
$ActualIP = (Invoke-WebRequest -Uri "http://checkip.dyndns.com" -ErrorAction SilentlyContinue).Content -replace "[^\d\.]"
Write-Host "Actual IP is: '$ActualIP'."

If ((($ResolvedIP -as [IPAddress]) -as [Bool]) -and (($ActualIP -as [IPAddress]) -as [Bool]) -and ($CachedIP -ne $ResolvedIP) -and ($ActualIP -ne $ResolvedIP)) {
Write-Host "Your IP adresses does not match up"
Write-Host "Updating your DNS"
if ($ActualIP -ne $ResolvedIP) {
$Update = (Invoke-WebRequest -Uri $UpdateURL -ErrorAction SilentlyContinue).Content
If ($Update.Substring(0,2) -ne "OK") {
Write-Host "Update status was not 'OK'. Resetting Cached IP. Request returned: $Update"
Write-Output "0.0.0.0" | Out-File -FilePath $IPCacheFile
}
Else {
Write-Host "Update status was OK. Updating Cached IP."
Write-Output $ActualIP | Out-File -FilePath $IPCacheFile
}
}
Else {
Write-Host "Actual IP '$ActualIP' already matches Resolved IP '$ResolvedIP'. Updating Cached IP."
Write-Output $ActualIP | Out-File -FilePath $IPCacheFile
}
}
Write-Host "Script completed."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment