-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-ddns.ps1
55 lines (45 loc) · 1.97 KB
/
batch-ddns.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# This script is based on Andrew Bresciano's script
# You can find it here: http://andrewbresciano.com/about-me/personal-projects/googles-dynamic-dns-powershell-script
# Set csv file location here
$csvLocation = 'C:\Users\YOUR\FILE\PATH.csv'
# csv headers
# hostname,user,pass
# Enable result logging - set to 1 to enable else disables it
$doLogging = 1
$logPath = 'C:\Users\YOUR\FILE\PATH.txt'
#import csv list
$csvFile = import-csv $csvLocation
$csvFile | ForEach-Object {
$hostname = $_.hostname
$user = $_.user
$pass = $_.pass
# Basic Auth Formatting
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
# Get Current Authoratative IP For DynDNS Hostname
$NS = (Resolve-DnsName $hostname -Server 8.8.8.8 -Type NS).PrimaryServer
$ipCurrent = (Resolve-DnsName $hostname -Server $NS -Type A).IPAddress
# Get Current Public IP
$ipPub = (curl -uri 'https://ipv4.wtfismyip.com/text').Content.Trim()
# You can use your preferred provider, I use wtfismyip.com because lols
# Output
"Host " + $hostname + " currently: " + $ipCurrent
"Current machine IP: " + $ipPub
# Set url destination
$URL = "https://domains.google.com/nic/update?hostname=" + $hostname + "&myip=" + $ipPub
# Logic
if ($ipCurrent -eq $ipPub) {
"IP still aligned. Does not need updating."
if ($doLogging -eq 1) {
Write-Output "$(Get-Date -Format "yyyy/MM/dd HH:mm") : $hostname $ipCurrent does not require update" | Out-File $logPath -Append -Encoding utf8
}
}
Else {
"Updating from: " + $ipCurrent + " to: " + $ipPub
$result = Invoke-RestMethod $URL -Credential $credential
"Server Response: " + $result
if ($doLogging -eq 1) {
Write-Output "$(Get-Date -Format "yyyy/MM/dd HH:mm") : $hostname $result from $ipCurrent" | Out-File $logPath -Append -Encoding utf8
}
}
}