Wrong dns record identifier updating

Hi,
I have a Powershell script to update two DNS records. Basically, I want dynamic DNS and I want to update both my A record and my AAAA record for a domain with the name ~nc.domain.com~

The script looks something like this:

$Headers=@{}
$Headers.Add("Authorization", "Bearer $ApiKey")

$RecordData = @{
   type = $Type
   name = $DomainName
   content = $Content
   comment = "Updated by Powershell script"
}

$RecordResponse = Invoke-RestMethod -Uri $api/dns_records/$Identifier -Method Put -Headers $Headers -Body ($RecordData | ConvertTo-Json)

I found the identifier using a dns_records GET request

The thing is it seems to update the first record with the name ~nc.domain.com~ regardless of whether it matches the identifier or not.
I first update the A record and it succeeds and then I update the AAAA record and it succeeds (as in the API returns success=True), each one has a different identifier but the same identifier is always updated regardless of which one is specified in the URL.

Any help would be appreciated. Thanks.

Hi CColes,

It appears that you are experiencing issues when trying to update DNS records using the Cloudflare API. The problem you’re encountering, where the same identifier is being updated regardless of which one is specified in the URL, may be due to a problem in the way you’re constructing the API request.

Here’s a revised version of your PowerShell script to update both A and AAAA records for the same DNS name (nc.domain.com) with different identifiers:

# Set your Cloudflare API endpoint
$api = "https://api.cloudflare.com/client/v4"

# Set your Cloudflare API key
$ApiKey = "YOUR_API_KEY"

# DNS record details
$DomainName = "~nc.domain.com~"
$Type = "A"  # Change to "AAAA" for AAAA records
$Content = "New_IP_Address"
$Comment = "Updated by Powershell script"

# Function to update a DNS record
function UpdateDnsRecord($recordId, $recordData) {
    $headers = @{
        "Authorization" = "Bearer $ApiKey"
    }

    $uri = "$api/zones/YOUR_ZONE_ID/dns_records/$recordId"  # Replace YOUR_ZONE_ID

    $response = Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -Body ($recordData | ConvertTo-Json)

    # Check for success
    if ($response.success) {
        Write-Host "DNS record with ID $recordId updated successfully."
    } else {
        Write-Host "Failed to update DNS record with ID $recordId."
        Write-Host "Errors: $($response.errors)"
    }
}

# Get the identifier of the DNS record to update
$recordsResponse = Invoke-RestMethod -Uri "$api/zones/YOUR_ZONE_ID/dns_records?name=$DomainName&type=$Type" -Headers $headers
$records = $recordsResponse.result

foreach ($record in $records) {
    $recordId = $record.id
    $recordData = @{
        type = $Type
        name = $DomainName
        content = $Content
        ttl = $record.ttl
        proxied = $record.proxied
        locked = $record.locked
        comment = $Comment
    }
    
    UpdateDnsRecord $recordId $recordData
}

In this script:

  • You first retrieve the DNS records for the specified domain and type (A or AAAA).
  • Then, you iterate through the records and update each one separately using the correct identifier.

Please make sure to replace YOUR_API_KEY with your actual API key and YOUR_ZONE_ID with your zone ID. Also, remember to handle the API endpoint correctly based on your Cloudflare configuration. This revised script should help you update both A and AAAA records for the same DNS name using the respective identifiers.

Hi @elliot123
Thanks for your response. I was doing some updates to my script based on your reply but then I found the problem. I had a typo in the input parameters to the update function so $Identifier was not being updated instead $Identifer was being updated. That explains why the same record was updated twice.

Also I’m not sure your script would work exactly as written because for the DNS records query I have found that I need to include "Content-Type" = "application/json" in the header also.
I post the entire working script here in case anyone is interested:

# Cloudflare API credentials
$api = "https://api.cloudflare.com/client/v4"
$ApiKey = "ApIkEy"  # Your Cloudflare API key
$ZoneId = "your_zone_id"  # Your Cloudflare zone ID
$identifierv4 = "your_v4_identifier" # Got by doing a dns_records GET request
$identifierv6 = "your_v6_identifier"
$DomainName = "nc.domain.com"  # Your domain name
$Headers=@{
    "Authorization" = "Bearer $ApiKey"
    "Content-Type" = "application/json"
}


# Function to get public IP addresses
function Get-PublicIps {
    $ipv4 = (Invoke-RestMethod -Uri "https://api.ipify.org?format=json").ip
    $ipv6 = (Invoke-RestMethod -Uri "https://api64.ipify.org?format=json").ip
    return $ipv4, $ipv6
}

# Function to update Cloudflare A and AAAA records
function Update-CloudflareRecords {
    param (
        [string]$Type,
        [string]$Content,
        [string]$Identifier
    )

    $RecordData = @{
        type = $Type
        name = $DomainName
        content = $Content
        comment = "Updated by Powershell script"
    }

    Write-Host ($RecordData | ConvertTo-Json)

    $RecordResponse = Invoke-RestMethod -Uri "$api/zones/$ZoneId/dns_records/$Identifier" -Method Put -Headers $Headers -Body ($RecordData | ConvertTo-Json)

    if ($RecordResponse.success) {
        Write-Host "$Type Record Update Success= $($RecordResponse.success)"
    } else {
        Write-Host "Failed to update DNS record with ID $Identifier."
        Write-Host "Errors: $($RecordResponse.errors)"
    }
}

$ipv4, $ipv6 = Get-PublicIps
Write-Host "Type: A, IP: $ipv4, Identifier: $identifierv4"
#Update-CloudflareRecords -Type "A" -Content $ipv4 -Identifier $identifierv4
Write-Host "Type: AAAA, IP: $ipv6, Identifier: $identifierv6"
#Update-CloudflareRecords -Type "AAAA" -Content $ipv6 -Identifier $identifierv6

$response = Invoke-RestMethod -Uri "$api/zones/$ZoneId/dns_records" -Method GET -Headers $Headers

foreach ($record in $($response.result))
{
    if ($record.name -eq "$DomainName"){
        Write-Host "id:$($record.id) name:$($record.name) type:$($record.type) content:$($record.content)"
    }
}

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.