daily pastebin goal
56%
SHARE
TWEET

cloudflare ad sync script

a guest Nov 12th, 2016 161 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $domain_to_sync_list = ("<define domain on ad to sync>")
  2. $email = "<email from cloudflare>"
  3. $api_key = "<api key from cloudflare>"
  4.  
  5. #When set to True deletes extra DNS entries that do not match AD
  6. $strict = $false
  7.  
  8. function get-cfzoneid() {
  9. Param(
  10.     [string] $DNSname
  11. )
  12. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  13. $headers.Add("Content-Type", "application/json")
  14. $headers.Add("X-Auth-Key", "$api_key")
  15. $headers.Add("X-Auth-Email", "$email")
  16.  
  17.     if($result = invoke-restmethod -Uri "https://api.cloudflare.com/client/v4/zones/" -Method GET  -headers $headers | select result) {
  18.         if($DNSname.count -ge 1) {
  19.             $dns_tmp = $DNSname.split(".")
  20.             $zone = $dns_Tmp[$dns_tmp.count-2] + "." + $dns_Tmp[$dns_tmp.count-1]
  21.         }
  22.        
  23.         if ($id = $result.result | where { $_.name -match $zone} | select -expandproperty id) {
  24.             return $id
  25.         } else {
  26.             return $false
  27.         }
  28.     }
  29. }
  30.  
  31. function create-CFdns() {
  32. Param(
  33.     [Parameter(Mandatory=$true)]
  34.     [string] $DNSname,
  35.     [Parameter(Mandatory=$true)]
  36.     [string] $type,
  37.     [Parameter(Mandatory=$true)]
  38.     [string] $ip_update,
  39.     [string] $id="NS"
  40.   )
  41. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  42. $headers.Add("Content-Type", "application/json")
  43. $headers.Add("X-Auth-Key", "$api_key")
  44. $headers.Add("X-Auth-Email", "$email")
  45. $uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
  46.  
  47.     if(-not($result = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET  -headers $headers)) {
  48.         return $false
  49.     }
  50.  
  51.     if(-not ($result.result | where { $_.name -eq "$DNSname"})) {
  52.         try {
  53.             $json = "{""type"":""" + $type +""",
  54.                      ""name"":""" + $dnsname + """,
  55.                      ""content"":""" + $ip_update + """ }"
  56.             $result = invoke-restmethod -Uri "https://api.cloudflare.com/client/v4/zones/$id/dns_records/" -Method POST -Body $json -headers $headers
  57.             return $result.result
  58.         } catch {
  59.             return $false
  60.         }
  61.     } else {
  62.         return $false
  63.     }
  64. }
  65.  
  66. function update-CFdns() {
  67. Param(
  68.   [Parameter(Mandatory=$true)]
  69.   [string] $DNSname,
  70.   [Parameter(Mandatory=$true)]
  71.   [string] $ip_update,
  72.   [string] $id="NS"
  73.   )
  74.  
  75. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  76. $headers.Add("Content-Type", "application/json")
  77. $headers.Add("X-Auth-Key", "$api_key")
  78. $headers.Add("X-Auth-Email", "$email")
  79. $uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
  80.  
  81.     if($id -eq "NS") {
  82.     $id = get-cfzoneid $DNSname
  83.     }
  84.  
  85.     if(-not ($result = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET  -headers $headers)) {
  86.     return $false
  87.     }
  88.  
  89.     if($data = $result.result | where { $_.name -eq "$DNSname"}) {
  90.         try {
  91.             $data | add-member "content" "$ip_update" -force
  92.             $json = $data | ConvertTo-Json
  93.             $query_url = $uri_base + "/dns_records/" + $data.id
  94.             $result = invoke-restmethod -Uri $query_url -Method PUT -Body $json -headers $headers
  95.             return $result.result
  96.         } catch {
  97.             return $false
  98.         }
  99.     } else {
  100.         return $false
  101.     }
  102. }
  103.  
  104. function delete-CFdns() {
  105. Param(
  106.   [Parameter(Mandatory=$true)]
  107.   [string] $DNSname,
  108.   [string] $id="NS"
  109.   )
  110.  
  111. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  112. $headers.Add("Content-Type", "application/json")
  113. $headers.Add("X-Auth-Key", "$api_key")
  114. $headers.Add("X-Auth-Email", "$email")
  115.  
  116.     if($id -eq "NS") {
  117.     $id = get-cfzoneid $DNSname
  118.     }
  119.     $uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
  120.     if(-not ($result = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET  -headers $headers)) {
  121.     return $false
  122.     }
  123.  
  124.     if($data = $result.result | where { $_.name -eq "$DNSname"}) {
  125.         try {
  126.             $query_url = $uri_base + "/dns_records/" + $data.id
  127.             $result = invoke-restmethod -Uri $query_url -Method DELETE -headers $headers
  128.             return $result.result
  129.         } catch {
  130.             return $false
  131.         }
  132.     } else {
  133.         return $false
  134.     }
  135. }
  136.  
  137. function get-cfdnslist() {
  138.     Param(
  139.   [Parameter(Mandatory=$true)]
  140.   [string] $id
  141.   )
  142.  
  143.     $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  144.     $headers.Add("Content-Type", "application/json")
  145.     $headers.Add("X-Auth-Key", "$api_key")
  146.     $headers.Add("X-Auth-Email", "$email")
  147.     $uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
  148.     $cf_dns_list = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET  -headers $headers
  149.     return $cf_dns_list.result
  150. }
  151.  
  152. foreach($domain in $domain_to_sync_list) {
  153. $dns_list = Get-DnsServerResourceRecord -ZoneName "$domain" | where {($_.RecordType -eq "A") -or ($_.RecordType -eq "CNAME")}
  154. $id = get-cfzoneid $domain
  155.     foreach($dns_entry in $dns_list) {
  156.         if(($dns_entry.HostName -ne "@") -AND ($dns_entry.HostName -ne "domaindnszones") -AND ($dns_entry.HostName -ne "forestdnszones")) {
  157.             switch($dns_entry.RecordType) {
  158.                 "A" {
  159.                         if(-not(update-CFdns $dns_entry.Hostname $dns_entry.RecordData.IPv4Address.IpaddressToString $id)){
  160.                         create-CFdns $dns_entry.Hostname $dns_entry.RecordType $dns_entry.RecordData.IPv4Address.IpaddressToString $id
  161.                         }
  162.                 }
  163.                
  164.                 "CNAME" {
  165.                     if(-not(update-CFdns $dns_entry.Hostname $dns_entry.RecordData.HostnameAlias $id)){
  166.                     create-CFdns $dns_entry.Hostname $dns_entry.RecordType $dns_entry.RecordData.HostnameAlias $id
  167.                     }
  168.                 }
  169.             }
  170.         }  
  171.     }
  172.    
  173.     if($strict) {
  174.     $cf_dns_list = get-cfdnslist $id
  175.         foreach($entry in $cf_dns_list.result.name) {
  176.         $replace_string = "." + $domain
  177.         $verify = $entry -Replace $replace_string
  178.             if($dns_list.Hostname -notcontains "$verify") {
  179.             delete-CFdns $entry $id
  180.             }
  181.         }
  182.     }
  183. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top