Skip to content

Instantly share code, notes, and snippets.

@mollyporph
Last active March 31, 2017 05:22
Show Gist options
  • Save mollyporph/c16f3997e2a2cfdf4e81 to your computer and use it in GitHub Desktop.
Save mollyporph/c16f3997e2a2cfdf4e81 to your computer and use it in GitHub Desktop.
A script to update dns-records at name.com
#---------STUFF THAT YOU SHOULD EDIT--
$username = "yourusername"
$apitoken = "yourapitoken" #Get an api-token at https://www.name.com/reseller usually doesn't take long
$domain = "yourdomain" #e.g example.com
$hostname = "yourhostname" #The hostname that you want to update, for mail.example.com the hostname should be "mail"
#
#------------------------------------
$headers = @{"Api-Username"= $username;"Api-Token" = $apitoken}
$listUrl = "https://api.name.com/api/dns/list/$domain"
$deleteUrl ="https://api.name.com/api/dns/delete/$domain"
$createUrl = "https://api.name.com/api/dns/create/$domain"
$ip = irm https://api.ipify.org?format=json
$res = irm $listUrl -Method Get -ContentType "application/json" -Headers $headers
$fqdn = $hostname+"."+$domain
$contentIds = $res.records | where name -eq $fqdn | select record_id
if($contentIds)
{
$contentIds.record_id | % {
$body = @{record_id = $PSItem}
irm $deleteUrl -Body (Convertto-json $body)-Method Post -Headers $headers -ContentType "application/json"
}
}
#for some types (like MX-pointers), you need to add a "priority"-property aswell
$body = [ordered]@{hostname = $hostname; type = "A"; content = $ip.ip;ttl = 300} #[ordered] prevents the values of the hashtable to jump around.
irm $createUrl -Body (Convertto-json $body) -Method Post -Headers $headers -ContentType "application/json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment