====== Update DuckDNS record using Powershell ====== You can use PowerShell to compare your current public IP with the IP resolved by your DuckDNS domain. If they differ, you'll update the IP and log the change. Here's the complete PowerShell script to do that: #Requires -Version 3.0 <# .SYNOPSIS Checks the current public IP, compares it with the DuckDNS resolved IP, and updates if necessary. .DESCRIPTION This script fetches the current public IP address, compares it with the IP address resolved from the DuckDNS domain, and updates DuckDNS if the IPs are different. It logs the changes to a local file. .PARAMETER Domains A comma-separated list of your DuckDNS domains to update. .PARAMETER Token Your DuckDNS token. .PARAMETER LogFile The path to the log file where changes will be recorded. .INPUTS None. You cannot pipe objects to this script. .OUTPUTS None. This script does not generate any output. .EXAMPLE .\Update-DuckDNS.ps1 -Domains "remotestudiocont" -Token my-duck-dns-token -LogFile "C:\Path\To\logfile.txt" #> Param ( [Parameter(Mandatory=$True)] [ValidateNotNullOrEmpty()] [String]$Domains, [Parameter(Mandatory=$True)] [ValidateNotNullOrEmpty()] [String]$Token, [Parameter(Mandatory=$True)] [ValidateNotNullOrEmpty()] [String]$LogFile ) Function Get-PublicIP { try { $ip = (Invoke-RestMethod -Uri "https://api.ipify.org").ToString() return $ip } catch { Throw "Failed to retrieve public IP." } } Function Get-DuckDNSResolvedIP { param ( [String]$Domain ) try { $ip = [System.Net.Dns]::GetHostAddresses($Domain).IPAddressToString return $ip } catch { Throw "Failed to resolve DuckDNS domain." } } Function Update-DuckDNS { param ( [String]$Domains, [String]$Token, [String]$IP ) $URL = "https://www.duckdns.org/update?domains={0}&token={1}&ip={2}" -f $Domains, $Token, $IP try { $Result = Invoke-WebRequest -Uri $URL -UseBasicParsing if ($Result.Content -eq "OK") { return $true } else { return $false } } catch { Throw "Failed to update DuckDNS." } } # Fetch current public IP $PublicIP = Get-PublicIP Write-Host "Current Public IP: $PublicIP" # Resolve DuckDNS domain IP $DuckDNSIP = Get-DuckDNSResolvedIP -Domain "$($Domains.Split(',')[0]).duckdns.org" Write-Host "DuckDNS Resolved IP: $DuckDNSIP" # Compare IPs and update if different if ($PublicIP -ne $DuckDNSIP) { Write-Host "IPs are different. Updating DuckDNS..." if (Update-DuckDNS -Domains $Domains -Token $Token -IP $PublicIP) { Write-Host "DuckDNS update successful." $LogEntry = "{0} - Updated DuckDNS from {1} to {2}" -f (Get-Date), $DuckDNSIP, $PublicIP Add-Content -Path $LogFile -Value $LogEntry } else { Write-Host "DuckDNS update failed." } } else { Write-Host "IPs are the same. No update needed." } ==== Explanation: ==== 1. **Functions**: * `Get-PublicIP`: Uses `Invoke-RestMethod` to get the current public IP from `https://api.ipify.org`. * `Get-DuckDNSResolvedIP`: Uses DNS resolution to get the IP address of the DuckDNS domain. * `Update-DuckDNS`: Uses `Invoke-WebRequest` to update the DuckDNS IP. 2. **Workflow**: * The script retrieves the current public IP. * It resolves the DuckDNS domain to get its current IP. * If the two IPs are different, it updates DuckDNS and logs the change. 3. **Logging**: * Changes are logged to a specified file (`LogFile` parameter) with the date and time of the update. ==== Usage: ==== Save the script to a `.ps1` file and run it with the necessary parameters. For example: .\Update-DuckDNS.ps1 -Domains "remotestudiocont" -Token "your-duck-dns-token" -LogFile "C:\Path\To\logfile.txt" This will ensure your DuckDNS IP is always up-to-date and changes are logged appropriately.