我今天也遇到了同样的问题,我对在这里或在Google上看到的答案不满意,因此我编写了一个PowerShell脚本,每当我的IP地址发生变化时,我都会向我发送Slack通知。
如果您希望接收电子邮件,则可以单击脚本中的链接以查看支持Outlook电子邮件的其他版本。
我希望这可以帮助某人并赢得投票。:-)
将以下文本保存到.ps1文件中。使用您自己的Slack webhook URL对其进行适当的编辑。救。右键单击该文件以“使用PowerShell运行”。
或者,您可以安排它每天或经常运行。
#Script to compare current IP with old IP and sends Slack notification if different (and do nothing if there was no change).
#We can put this as a scheduled task to run daily.
#ScriptName: IP_change_detection_notification.ps1
$slackWebhookUrl = "XXXXXXXXXX" #put yours here
$ipDetectionUrl = "https://wtfismyip.com/text"
$IPAddFile = "C:\code\IP_change_detection_notification.dat" #absolute path to file that stores the old IP record
$slackOutputFile = "C:\code\IP_change_detection_notification_Slack.txt"
$optionalDebuggingFile = "C:\code\IP_change_detection_notification_debugging.txt"
$Request = Invoke-WebRequest $ipDetectionUrl
$IP_new = ($Request.Content.Trim())
Write-Host "Current IP address: [$IP_new]"
#Check if old IP record exists
If(Test-Path "$IPAddFile")
{
#Get old IP
$IP_old = Get-Content "$IPAddFile"
#Compare IPs
if(-not($IP_new -eq $IP_old))
{
Write-Host "Old IP address: [$IP_old]"
$msg = "Your WAN IP has changed to $IP_new (was $IP_old)!"
Write-Host "$msg"
$body = $("{""text"":""$msg""}")
Write-Host "$body"
Invoke-RestMethod -Uri $slackWebhookUrl -Method Post -ContentType 'application/json' -Body $body -OutFile $slackOutputFile
"Notification Sent"
#Overwrite and update new IP
$IP_new | Out-File $IPAddFile
}
else
{"No change, no notification"}
}
else
{
#Create new, as file not found
$IP_new | Out-File $IPAddFile
"File created"
}
$(get-date -f yyyy-MM-dd_HH_mm_ss) | Out-File $optionalDebuggingFile
#Read-Host -Prompt "Press Enter to exit" #Comment out this line if this script will be run by a cron job. Otherwise, uncomment it so that you can see the results of the script in the console.
#This script was adapted from https://gallery.technet.microsoft.com/scriptcenter/Detect-IP-address-change-aeb51118 by Satyajit
要使任务计划程序正常工作:
我必须以管理员身份运行PowerShell,然后运行Get-ExecutionPolicy
,然后告诉我当前的ExecutionPolicy为“受限”。
然后我跑了Set-ExecutionPolicy RemoteSigned
(如此处所示,但这让我感到紧张:https : //stackoverflow.com/a/26955050/470749)。
然后从基本的Windows命令提示符下,尝试几次运行以下命令:(C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -File "C:\code\IP_change_detection_notification.ps1"
一次存储IP,第二次检查它是否已更改)。
(直到您使它起作用,不要再尝试使用Task Scheduler。)
然后,我用C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
程序和-ExecutionPolicy ByPass -File C:\code\IP_change_detection_notification.ps1
参数安排了一个任务。