ddns-go更新的频率高,不论是Linux还是Windows,手动更新都是比较麻烦的,用脚本自动执行来代替手动,是更好的解决方法。
我已经编写了一个Linux shell脚本,可以在Linux主机上半自动安装、更新ddns-go,效果还不错。
同理,在Windows上,也可以通过脚本实现自动更新ddns-go。
如果你的Windows系统也有根据IP动态更新DNS记录的需求,本文通过PowerShell+计划任务让ddns-go可以自动更新。
创建一个PowerShell脚本,位置在:C:\ddns\update-ddns-go.ps1
内容如下:
# =============================
# ddns-go 自动更新脚本
# PowerShell 5.1 兼容版
# =============================
# 设置变量
$AppName = "ddns-go"
$InstallDir = "C:\ddns"
$ExePath = Join-Path $InstallDir "ddns-go.exe"
$ZipPath = Join-Path $InstallDir "ddns-go.zip"
$LastRunFile = Join-Path $InstallDir "last_run_week.txt"
$ApiUrl = "https://api.github.com/repos/jeessy2/ddns-go/releases/latest"
# 确保目录存在
if (!(Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir | Out-Null
}
# -----------------------------
# ISO 周数兼容函数(Win10 可用)
# -----------------------------
function Get-ISOWeek {
param([datetime]$Date)
$day = [int]$Date.DayOfWeek
if ($day -eq 0) { $day = 7 } # Sunday = 7
# 移动到同一周的星期四
$thursday = $Date.AddDays(4 - $day)
# 该年份的第一个星期四
$firstThursday = Get-Date -Year $thursday.Year -Month 1 -Day 4
return [int](([math]::Floor(($thursday - $firstThursday).Days / 7)) + 1)
}
# -----------------------------
# 判断本周是否执行过更新
# -----------------------------
$Today = Get-Date
$WeekNumber = Get-ISOWeek -Date $Today
$YearWeek = "{0}-W{1:00}" -f $Today.Year, $WeekNumber
if (Test-Path $LastRunFile) {
$LastWeek = Get-Content $LastRunFile -ErrorAction SilentlyContinue
if ($LastWeek -eq $YearWeek) {
exit
}
}
# -----------------------------
# Windows Toast 通知函数
# -----------------------------
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function Show-Toast {
param(
[string]$Title,
[string]$Message
)
$notify = New-Object System.Windows.Forms.NotifyIcon
$notify.Icon = [System.Drawing.SystemIcons]::Information
$notify.Visible = $true
$notify.ShowBalloonTip(5000, $Title, $Message, [System.Windows.Forms.ToolTipIcon]::None)
}
# -----------------------------
# 获取最新版本
# -----------------------------
try {
$Response = Invoke-RestMethod -Uri $ApiUrl -UseBasicParsing -ErrorAction Stop
$LatestVersion = $Response.tag_name.TrimStart("v")
} catch {
Show-Toast -Title "$AppName 更新失败" -Message "无法获取最新版本"
exit
}
# -----------------------------
# 如果没有安装,视为版本 0
# -----------------------------
$CurrentVersion = ""
if (Test-Path $ExePath) {
try {
$CurrentVersion = (& $ExePath -v)
} catch {
$CurrentVersion = ""
}
}
# -----------------------------
# 判断是否需要更新
# -----------------------------
if ($CurrentVersion -eq $LatestVersion) {
Set-Content $LastRunFile $YearWeek
Show-Toast -Title "$AppName 已是最新" -Message "当前版本:$LatestVersion"
exit
}
# -----------------------------
# 下载最新 zip
# -----------------------------
$Asset = $Response.assets | Where-Object { $_.name -match "windows_x86_64.zip" }
$DownloadUrl = $Asset.browser_download_url
try {
Invoke-WebRequest -Uri $DownloadUrl -OutFile $ZipPath -UseBasicParsing -ErrorAction Stop
} catch {
Show-Toast -Title "$AppName 更新失败" -Message "下载失败"
exit
}
# -----------------------------
# 解压并替换程序
# -----------------------------
try {
Expand-Archive -Path $ZipPath -DestinationPath $InstallDir -Force
Remove-Item $ZipPath -Force
} catch {
Show-Toast -Title "$AppName 更新失败" -Message "解压失败"
exit
}
# -----------------------------
# 记录已更新的周次
# -----------------------------
Set-Content $LastRunFile $YearWeek
# -----------------------------
# 发送成功通知
# -----------------------------
Show-Toast -Title "$AppName 更新成功" -Message "已更新至版本 $LatestVersion"
管理员运行PowerShell,输入以下命令将脚本加入开机启动:
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File C:\ddns\update-ddns-go.ps1"
$Trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -TaskName "Update-ddns-go-weekly" -Action $Action -Trigger $Trigger -RunLevel Highest
效果:
- 每次开机自动检测最新版本、下载、更新 ddns-go
- 每周最多执行一次,本周已执行过开机就跳过
- 无论成功或失败,都使用 Windows Toast 通知
- 完全兼容 Win10
- 手动双击执行也可以正常运行
#ddns-go #powershell #windows