Cloudflarae ip优选Github地址https://github.com/XIU2/CloudflareSpeedTest

系统:Win10

如果在使用powershell时出现此系统禁止运行脚本这种错误,那么可以采用下面的步骤解决:

  1. 通过管理员权限 打开powershell

  2. 输入Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser 这条命令。

  3. 在出现的命令交互界面中输入A,敲回车。

  4. 重新打开powershell,即可运行脚本。


第一步,将以下内容复制到一个Cloudflarae ip优选同目录的新建TXT下

# 配置信息
$adguardUrl = "adguard的网页登录地址"
#例如:$adguardUrl = "http://192.168.8.20:3000"
$adguardUser = "Adguard的登录账号"
$adguardPass = "Adguard的登录密码"
$cloudflareToolDir = "Cloudflaraeip优选下载下来的exe文件所在目录"
#例如:$cloudflareToolDir = "C:\Users\PC5\Desktop\CloudflareSpeedTest-Adguard-Script-main"
$batchPath = "Cloudflaraeip优选下载下来的同目录下的run_speedtest.bat"
#例如:$batchPath = "C:\Users\PC5\Desktop\CloudflareSpeedTest-Adguard-Script-main\run_speedtest.bat"
$domains = @(
    "blog.rabbitcutest.top",
    "blog.rabbitcutest.top",
    "blog.rabbitcutest.top"
    # 在此添加更多域名...
)

# 基础认证处理
$authInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${adguardUser}:${adguardPass}"))
$headers = @{
    "Authorization" = "Basic $authInfo"
    "Content-Type" = "application/json"
}

# 1. 使用指定的批处理文件运行Cloudflare优选IP测试
Write-Host "正在使用批处理文件运行Cloudflare优选IP测试..." -ForegroundColor Cyan
$process = Start-Process cmd.exe -ArgumentList "/c `"$batchPath`"" -Wait -WindowStyle Normal -PassThru

if ($process.ExitCode -ne 0) {
    Write-Host "测速过程出错,退出代码: $($process.ExitCode)" -ForegroundColor Red
    exit
}

Write-Host "优选IP测试完成" -ForegroundColor Green
Write-Host ""

# 2. 解析结果文件获取最优IP
$resultFile = "$cloudflareToolDir\result.csv"
if (-not (Test-Path $resultFile)) {
    Write-Host "错误:未找到结果文件 $resultFile" -ForegroundColor Red
    exit
}

# 直接读取CSV内容(不依赖列名)
$csvLines = Get-Content $resultFile | Where-Object { $_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' }
if (-not $csvLines) {
    Write-Host "错误:结果文件中未找到有效IP" -ForegroundColor Red
    exit
}

# 获取第一个有效IP(最优IP)
$bestIP = $csvLines[0] -split ',' | Select-Object -First 1
Write-Host "获取到最优IP: $bestIP" -ForegroundColor Cyan

# 3. 获取现有DNS重写规则
try {
    $rewritesUrl = "$adguardUrl/control/rewrite/list"
    $currentRules = Invoke-RestMethod -Uri $rewritesUrl -Method Get -Headers $headers
}
catch {
    Write-Host "获取现有规则失败: $_" -ForegroundColor Red
    exit
}

# 4. 删除所有现有规则
if ($currentRules) {
    Write-Host "正在清理现有DNS重写规则..." -ForegroundColor Yellow
    $deleteUrl = "$adguardUrl/control/rewrite/delete"
    
    foreach ($rule in $currentRules) {
        $deleteBody = @{
            domain = $rule.domain
            answer = $rule.answer
        } | ConvertTo-Json
        
        try {
            Invoke-RestMethod -Uri $deleteUrl -Method Post -Headers $headers -Body $deleteBody | Out-Null
            Write-Host "已删除: $($rule.domain) => $($rule.answer)" -ForegroundColor DarkYellow
        }
        catch {
            Write-Host "删除规则 $($rule.domain) 失败: $_" -ForegroundColor Red
        }
    }
    Write-Host "已清理所有现有规则" -ForegroundColor Green
}

# 5. 添加新规则
Write-Host "正在添加新的DNS重写规则..." -ForegroundColor Cyan
$addUrl = "$adguardUrl/control/rewrite/add"

foreach ($domain in $domains) {
    $ruleBody = @{
        domain = $domain
        answer = $bestIP
    } | ConvertTo-Json

    try {
        Invoke-RestMethod -Uri $addUrl -Method Post -Headers $headers -Body $ruleBody | Out-Null
        Write-Host "已添加: $domain => $bestIP" -ForegroundColor Green
    }
    catch {
        Write-Host "添加规则 $domain 失败: $_" -ForegroundColor Red
    }
}

Write-Host "`n所有操作已完成!" -ForegroundColor Green

第二步,将txt改名为update_adguard.ps1如下图


注意,建议另存为带BOM的UTF-8如下图

第三步,修改run_speedtest.bat文件,右键以记事本打开,修改为以下内容

@echo off
chcp 65001 > nul
title Cloudflare测速工具

echo 正在执行测速,请稍候...
echo. | CloudflareSpeedTest.exe -dn 6 -dd -o result.csv

echo 测速已完成!结果已保存到 result.csv
timeout /t 2 > nul
exit

第四步,按住shift+鼠标右键在此处打开Powershell

第五步,输入 .\update_adguard.ps1运行程序

 .\update_adguard.ps1

如果需要一段时间自动执行一次可以使用如下bat脚本

@echo off
setlocal enabledelayedexpansion

:: 配置参数
set "ps_script=update_adguard.ps1文件的完整路径"
:: 例如set "ps_script=C:\Users\PC5\Desktop\CloudflareSpeedTest-Adguard-Script-main\update_adguard.ps1"
set /a interval_seconds=604800  :: 7天 = 604800秒

:: 检查记录文件是否存在
if not exist last_run.txt (
    echo 第一次运行,执行脚本...
    powershell -ExecutionPolicy Bypass -File "%ps_script%"
    echo %date% %time% > last_run.txt
)

:main_loop
:: 读取上次执行时间
set "last_run="
for /f "usebackq delims=" %%t in ("last_run.txt") do set "last_run=%%t"

:: 计算下次执行时间
for /f %%i in ('powershell -command "(Get-Date '%last_run%').AddSeconds(%interval_seconds%)"') do (
    set "next_run=%%i"
)

:: 主循环
:check_time
for /f %%d in ('powershell -command "((Get-Date '%next_run%') - (Get-Date)).TotalSeconds"') do (
    set /a "remaining=%%d"
)

:: 如果到达执行时间
if !remaining! leq 0 (
    echo 正在执行脚本...
    powershell -ExecutionPolicy Bypass -File "%ps_script%"
    echo %date% %time% > last_run.txt
    timeout /t 5 /nobreak >nul
    goto main_loop
)

:: 计算时间单位
set /a "days=remaining/86400"
set /a "hours=(remaining%%86400)/3600"
set /a "minutes=(remaining%%3600)/60"
set /a "seconds=remaining%%60"

:: 显示倒计时
cls
echo ===============================================
echo  下次执行时间: %next_run%
echo  剩余时间: %days%天 %hours%小时 %minutes%分 %seconds%秒
echo ===============================================
echo  按 CTRL+C 可终止脚本运行
echo  正在等待下一次执行...

:: 动态等待(每秒更新)
timeout /t 1 /nobreak >nul
goto check_time

注意:bat脚本建议另存为为ANSI编码,这样就不会出现乱码了

运行流程图片,全部自动运行,不需要任何手动