# ============================================================ # 安装 MyBlog IP 白名单定时任务(家庭 Windows 电脑) # 作用:注册 Windows 计划任务,每 30 分钟自动运行 report.ps1 # 前置:已复制 ipwatch.conf.example 为 ipwatch.conf 并填写 # 用法:powershell -ExecutionPolicy Bypass -File .\install.ps1 # 说明:计划任务在“当前登录用户”下运行;如需开机无登录也运行, # 请右键任务 -> 属性勾选“不管用户是否登录都要运行” # ============================================================ $ErrorActionPreference = "Stop" $ScriptDir = $PSScriptRoot $Report = Join-Path $ScriptDir "report.ps1" $ConfFile = Join-Path $ScriptDir "ipwatch.conf" if (-not (Test-Path -LiteralPath $ConfFile)) { Write-Host "缺少 ipwatch.conf:请先复制 ipwatch.conf.example 并填写 SERVER / SECRET" -ForegroundColor Red exit 1 } $TaskName = "MyBlogIPWatch" # /TR 里对带空格的脚本路径加引号,PowerShell 会整体作为一个参数传给 schtasks $Action = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$Report`"" & schtasks /Create /F /TN $TaskName /SC MINUTE /MO 30 /TR $Action | Out-Null if ($LASTEXITCODE -ne 0) { Write-Host "创建计划任务失败,请确认以管理员身份运行或检查 schtasks 输出" -ForegroundColor Red exit 1 } # 创建后立即运行一次,马上验证配置是否正确 & schtasks /Run /TN $TaskName | Out-Null Write-Host "计划任务 $TaskName 已创建:每 30 分钟自动检查公网 IP。" Write-Host "日志文件:$(Join-Path $ScriptDir 'ipwatch.log')" Write-Host "(首次运行结果可在日志中查看;卸载请运行 uninstall.ps1)"