diff --git a/Discord.ps1 b/Discord.ps1 index 9fa0e73..02f2acc 100644 --- a/Discord.ps1 +++ b/Discord.ps1 @@ -14,7 +14,7 @@ # ### Example # Run from CMD or Batch file to transmit the message to Discord: -# CMD> powershell.exe -File .\Discord.ps1 -message "Here is some content to send to Discord." +# CMD> pwsh -File .\Discord.ps1 -message "Here is some content to send to Discord." # or pipeline in powershell: # PS1> echo "Here is some content to send to Discord" | .\Discord.ps1 # @@ -27,9 +27,9 @@ param ( [parameter (ValueFromPipeline = $true)] [string[]] $message, - [string] $distinct, - [string] $webhook, - [string] $botName + [string] $distinct = "", + [string] $webhook = "", + [string] $botName = "" ) begin { $chunk = "" @@ -66,6 +66,7 @@ end { # complete last chunk, by queuing it up $chunks += $chunk + # if distinct, load previous message to check for duplicate message if ($distinct) { $distinctFile = "$PSScriptRoot\\distinct\\$distinct.msg" if (Test-Path -Path $distinctFile) { diff --git a/Dynv6.ps1 b/Dynv6.ps1 index b60610e..6add829 100644 --- a/Dynv6.ps1 +++ b/Dynv6.ps1 @@ -4,18 +4,19 @@ # # Windows Powershell Script for updating Dynamic DNS on Dynv6.com using the # RESTful interface. Use the windows task schedualer to automatically run. +# Will use config.ps1 for defaults, if available. # # Script Parameters: -# -zone DNS Zone (domain name) to update. -# -token HTTP Token, see: https://dynv6.com/keys#token +# [-zone] Optional, DNS Zone (domain name) to update, default to config. +# [-token] Optional, HTTP Token, see: https://dynv6.com/keys#token defaults to config. # [-netmask] Optional, IPv6 netmask, defaults to 128. -# [-logFile] Optional, filename to log to, defaults to "~/.dynv6.log", use null to disable logging. +# [-logFile] Optional, filename to log to, defaults to "~\.dynv6.log", use $null to disable logging. # [-ipv4] Optional, IPv4 override, to skip resolving. # [-ipv6] Optional, IPv6 override, to skip resolving. # ### Example # Run from CMD or Batch file to IPv4 and IPv6 with automatic lookup: -# powershell.exe -File ./Dynv6.ps1 -zone "ExampleDomain.com" -token "XXXXXX" +# CMD> pwsh -File .\Dynv6.ps1 -zone "ExampleDomain.com" -token "XXXXXX" # ################### # Copyright 2020 Joe Herbert ( djneonc@gmail.com ) @@ -41,14 +42,27 @@ # we need zone and token, we also accept, netmask, and overrides param( - [string] $zone = $(throw "-zone is required"), - [string] $token = $(throw "-token is required"), + [string] $zone = "", + [string] $token = "", [string] $netmask = "128", [object] $logFile = "$env:USERPROFILE\\.dynv6.log", [string] $ipv4 = "", [string] $ipv6 = "" ) +# load in defaults from config +$configFile = "$PSScriptRoot\\Config.ps1" +if (Test-Path -Path $configFile) { + . $configFile + + if ( ! $zone) { + $zone = $DYNV6_ZONE + } + if ( ! $token) { + $token = $DYNV6_TOKEN + } +} + # we store ip addresses in files, so we only update when there is a change $file4 = "$env:USERPROFILE\\.dynv6.addr4" $file6 = "$env:USERPROFILE\\.dynv6.addr6" @@ -56,13 +70,9 @@ $file6 = "$env:USERPROFILE\\.dynv6.addr6" # load existing ip addresses, when available if (Test-Path -Path $file4) { $old4 = Get-Content -Path $file4 -} else { - $old4 = $null } if (Test-Path -Path $file6) { $old6 = Get-Content -Path $file6 -} else { - $old6 = $null } $lookup4 = [System.Uri]'https://ipinfo.io/ip' diff --git a/SendDiscordChunk.ps1 b/SendDiscordChunk.ps1 index 23f046a..021be3c 100644 --- a/SendDiscordChunk.ps1 +++ b/SendDiscordChunk.ps1 @@ -1,4 +1,4 @@ -# internal function to transmit a discord chunk +# internal function to transmit a chunk of text to Discord function SendDiscordMessage { param ( @@ -17,19 +17,21 @@ function SendDiscordMessage $botName = ConvertTo-Json -InputObject $botName # rewrite json string, because "\u001b[m" needs to "\u001b[0m" instead - # windows is fine with the missing zero, but discord needs it + # windows is fine with the missing zero, but Discord needs it $message = $message -Replace "\\u001b\[m", "\u001b[0m" # remove wrapping quotes around json string, replace with color-enabled block quotes $message = '"```ansi\n' + $message.SubString(1, $message.length - 2) + '\n```"' - # {"username": "$botName", "content": "$message"} + # build post data: {"username": "$botName", "content": "$message"} $content = "{"; if ($botName) { $content += '"username": ' + $botName + ', '; } $content += '"content": ' + $message + '}'; - echo "$content" | curl.exe -s -H "$header" -X "POST" -d "@-" "$webhook" | ConvertFrom-Json | Select-Object -Property id | Out-Host + # post data is complex, so we have to pipe it into curl + # display the resulting id/message returned from Discord + echo "$content" | curl.exe -s -H "$header" -X "POST" -d "@-" "$webhook" | ConvertFrom-Json | Select-Object -Property id, message } diff --git a/config.example.ps1 b/config.example.ps1 index e8b9f45..b9f8c57 100644 --- a/config.example.ps1 +++ b/config.example.ps1 @@ -4,3 +4,7 @@ $DISCORD_SERVER_NAME = "" $DISCORD_WINDOWS_HOOK = "?wait=true" +$DYNV6_ZONE = "" + +$DYNV6_TOKEN = "" +