Skip to content

Instantly share code, notes, and snippets.

@Chirishman
Last active May 1, 2019 01:32
Show Gist options
  • Save Chirishman/fe4ae36aaf0988728d27b9cc10328f34 to your computer and use it in GitHub Desktop.
Save Chirishman/fe4ae36aaf0988728d27b9cc10328f34 to your computer and use it in GitHub Desktop.
Google Domains Dynamic DNS Updater
$ScheduledJob = @{
Name = 'DNS Updater'
Trigger = New-JobTrigger -Once -RepetitionInterval 0:15 -RepeatIndefinitely -At 10:30
Credential = (Get-Credential)
ScriptBlock = {
#Your info goes here
$hostname = "HOSTNAME.com"
$CurrentRecord = Resolve-DNSName -Name $hostname -Type A -Server 8.8.8.8 -DnsOnly | select -exp IPAddress
#Get a page with your current IP
$MyIp = Invoke-WebRequest "https://domains.google.com/checkip" | select -exp content
#Make sure we got a IP back in the response
If ($MyIp -match "(?:[0-9]{1,3}.){3}[0-9]{1,3}")
{
if ($MyIP -ne $CurrentRecord) { # Only update the IP if it has changed
$UpdateSplat = @{
#encode the username and password for the header
headers = @{
#Authorization = "Basic {0}" -f ([System.Convert]::ToBase64String(([System.Text.Encoding]::ASCII.GetBytes(((Get-StoredCredential -CredName 'DomainUpdateCreds').GetNetworkCredential() | %{($_.UserName,$_.password) -join ':'})))))
Authorization = "Basic {0}" -f ([System.Convert]::ToBase64String(([System.Text.Encoding]::ASCII.GetBytes((('USERNAMESTRING','PASSWORDSTRING') -join ':')))))
}
#Build up the URL
uri = "https://domains.google.com/nic/update?hostname={0}&myip={1}" -f $hostname, $MyIp
}
#Invoke the URL
$resp = Invoke-WebRequest @UpdateSplat
$resp.Content #Expected answers that I found "good","nochg","nohost","badauth","notfqdn"
} else {
Write-Verbose -Message 'No Change' -Verbose
}
}
Else
{
Write-Verbose -Message "No IP" -Verbose
}
}
}
Register-ScheduledJob @ScheduledJob
@Chirishman
Copy link
Author

To use line 21 instead of line 22 install this dependency: https://www.powershellgallery.com/packages/StoredPSCredential/2.0.4.0

And then run this to initialize the credentials:

Initialize-StoredCredential -CredName 'DomainUpdateCreds'

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