$logFile="C:\path\to\your\ip_log.txt"
$checkInterval=300 #检查间隔,单位为秒(这里设置为5分钟)
functionGet-ExternalIP{
try{
$ip=Invoke-RestMethod-Urihttp://ipinfo.io/ip
return$ip.Trim()
}
catch{
return$null
}
}
$lastIP=$null
while($true){
$currentIP=Get-ExternalIP
$currentTime=Get-Date-Format"yyyy-MM-ddHH:mm:ss"
if($currentIP-and($currentIP-ne$lastIP)){
$logMessage="$currentTime-IPchangedto:$currentIP"
Add-Content-Path$logFile-Value$logMessage
Write-Host$logMessage
$lastIP=$currentIP
}
Start-Sleep-Seconds$checkInterval
}
-----------------------------------------------------
网友回复:
引用:龙笑天发表于2024-9-2210:32
谢谢分享