log file can be set to null to disable logging.
This commit is contained in:
@@ -6,12 +6,12 @@
|
||||
# RESTful interface. Use the windows task schedualer to automatically run.
|
||||
#
|
||||
# Script Parameters:
|
||||
# -zone <string> DNS Zone (domain name) to update.
|
||||
# -token <string> HTTP Token, see: https://dynv6.com/keys#token
|
||||
# [-netmask] <string> Optional, IPv6 netmask, defaults to 128.
|
||||
# [-logfile] <string> Optional, filename to log to, defaults to "~/.dynv6.log".
|
||||
# [-ipv4] <string> Optional, IPv4 override, to skip resolving.
|
||||
# [-ipv6] <string> Optional, IPv6 override, to skip resolving.
|
||||
# -zone <string> DNS Zone (domain name) to update.
|
||||
# -token <string> HTTP Token, see: https://dynv6.com/keys#token
|
||||
# [-netmask] <string> Optional, IPv6 netmask, defaults to 128.
|
||||
# [-logFile] <string|null> Optional, filename to log to, defaults to "~/.dynv6.log", use null to disable logging.
|
||||
# [-ipv4] <string> Optional, IPv4 override, to skip resolving.
|
||||
# [-ipv6] <string> Optional, IPv6 override, to skip resolving.
|
||||
#
|
||||
### Example
|
||||
# Run from CMD or Batch file to IPv4 and IPv6 with automatic lookup:
|
||||
@@ -44,7 +44,7 @@ param(
|
||||
[string] $zone = $(throw "-zone is required"),
|
||||
[string] $token = $(throw "-token is required"),
|
||||
[string] $netmask = "128",
|
||||
[string] $logfile = "$env:USERPROFILE\\.dynv6.log",
|
||||
[object] $logFile = "$env:USERPROFILE\\.dynv6.log",
|
||||
[string] $ipv4 = "",
|
||||
[string] $ipv6 = ""
|
||||
)
|
||||
@@ -71,7 +71,11 @@ $lookup6 = [System.Uri]'https://v6.ipinfo.io/ip'
|
||||
$update4 = [System.Uri]'https://dynv6.com/api/update'
|
||||
$update6 = [System.Uri]'https://dynv6.com/api/update'
|
||||
|
||||
Get-Date | Tee-Object -FilePath $logfile -Append | Out-Host
|
||||
[string[]] $events = New-Object string[] 0
|
||||
|
||||
if ($logFile) {
|
||||
$events += Get-Date
|
||||
}
|
||||
|
||||
# lookup IPv4, will be skipped if variable already set
|
||||
if ( ! $ipv4) {
|
||||
@@ -86,16 +90,16 @@ $ipv6 = "$ipv6/$netmask"
|
||||
|
||||
# update IPv4, if changed
|
||||
if ($old4 -eq $ipv4) {
|
||||
Tee-Object -FilePath $logfile -Append -InputObject "IPv4 address unchanged" | Out-Host
|
||||
$events += "IPv4 address unchanged"
|
||||
} else {
|
||||
# send IPv4 address to dynv6
|
||||
Tee-Object -FilePath $logfile -Append -InputObject "Updating IPv4" | Out-Host
|
||||
$events += "Updating IPv4"
|
||||
$body = @{
|
||||
zone = $zone
|
||||
token = $token
|
||||
ipv4 = $ipv4
|
||||
}
|
||||
Invoke-RestMethod -Method Get -Uri $update4 -Body $body | Tee-Object -FilePath $logfile -Append | Out-Host
|
||||
$events += Invoke-RestMethod -Method Get -Uri $update4 -Body $body
|
||||
|
||||
# save current IPv4 address
|
||||
Out-File -FilePath $file4 -InputObject $ipv4
|
||||
@@ -103,18 +107,23 @@ if ($old4 -eq $ipv4) {
|
||||
|
||||
# update IPv6, if changed
|
||||
if ($old6 -eq $ipv6) {
|
||||
Tee-Object -FilePath $logfile -Append -InputObject "IPv6 address unchanged" | Out-Host
|
||||
$events += "IPv6 address unchanged"
|
||||
} else {
|
||||
# send IPv6 address to dynv6
|
||||
Tee-Object -FilePath $logfile -Append -InputObject "Updating IPv6" | Out-Host
|
||||
$events += "Updating IPv6"
|
||||
$body = @{
|
||||
zone = $zone
|
||||
token = $token
|
||||
ipv6 = $ipv6
|
||||
}
|
||||
Invoke-RestMethod -Method Get -Uri $update6 -Body $body | Tee-Object -FilePath $logfile -Append | Out-Host
|
||||
$events += Invoke-RestMethod -Method Get -Uri $update6 -Body $body
|
||||
|
||||
# save current IPv6 address
|
||||
Out-File -FilePath $file6 -InputObject $ipv6
|
||||
}
|
||||
|
||||
if ($logFile) {
|
||||
Out-File -FilePath $logFile -Append -InputObject ($events -join "`n")
|
||||
}
|
||||
echo ($events -join "`n")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user