forked from UpperHighway/Enable-DuckDns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnable-DuckDns.ps1
154 lines (133 loc) · 6.04 KB
/
Enable-DuckDns.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<#
.SYNOPSIS
Updates the IP address of your DuckDNS domain(s) on
a schedule you decide (in minutes).
.DESCRIPTION
This script registers two schedulded tasks automatically, one
which runs at system start, which will set up the other task
again in the event your system reboots, so you don't have to
remember to re-run this script. The second schedulded task runs
however often you set it to, and does the actual work of updating
your DuckDNS domains.
.PARAMETER Domains
A comma-separated list of your Duck DNS domains to update.
.PARAMETER Token
Your Duck DNS token.
.PARAMETER IP
The IP address to use. Usuallu DuckDNS automatically detects this
for you, so you should leave it blank unless you know what you're
doing.
.INPUTS
None.
.OUTPUTS
The script writes to the event log if it encounters problems with
writing to the DuckDNS web service.
.EXAMPLE
.\Enable-DuckDNS.ps1 -MyDomains "wibble,pibble" -Token YourDuckDNSToken -Interval 5
.LINK
#>
Param (
[Alias("Domain","Domains","MyDomains")]
[Parameter(
Mandatory=$True,
HelpMessage="Comma separate the domains if you want to update more than one."
)]
[String]$MyDomain,
[Alias("Token")]
[Parameter(Mandatory=$True)]
[String]$MyToken,
[Alias("Interval")]
[Parameter(Mandatory=$False)]
[int]$MyUpdateInterval = 5,
[Parameter(Mandatory=$False)]
[String]$IP = ""
)
# This scriptblock is the code which does the actual update call to the
# DuckDNS web service.
[scriptblock]$UpdateDuckDns = {
param(
[Parameter(Mandatory=$true,Position=0)]
[string]$strUrl
)
$Encoding = [System.Text.Encoding]::UTF8;
# Run the call to DuckDNS's website
$HTTP_Response = Invoke-WebRequest -Uri $strUrl;
# Turn the response into english ;)
$Text_Response = $Encoding.GetString($HTTP_Response.Content);
# If the response is anything other than 'OK' then log an error in the windows event log
if($Text_Response -ne "OK"){
Write-EventLog -LogName Application -Source "DuckDNS Updater" -EntryType Information -EventID 1 -Message "DuckDNS Update failed for some reason. Check your Domain or Token.";
} else {
Write-EventLog -LogName Application -Source "DuckDNS Updater" -EntryType Information -EventID 2 -Message "DuckDNS Update successful"
}
}
# This scriptblock is the code which gets run when the system starts up each time,
# and is responsible for setting up the job which will repeat every five minutes
# to update your IP address with DuckDNS
[scriptblock]$SetupRepeatingJob = {
param(
[Parameter(Mandatory=$true,Position=0)]
[string]$strDomain,
[Parameter(Mandatory=$true,Position=1)]
[string]$strToken,
[Parameter(Mandatory=$true,Position=2)]
[int]$iUpdateInterval,
[Parameter(Mandatory=$false,Position=3)]
[string]$strIP=""
)
# Build DuckDNS update URL using supplied domain, token and optional IP parameters
$duckdns_url = "https://www.duckdns.org/update?domains=" + $strDomain + "&token=" + $strToken + "&ip=" + $strIP;
# Set how often we want the job to repeat based on the interval set at the start of the script
$RepeatTimeSpan = New-TimeSpan -Minutes $iUpdateInterval;
# Set the time to start running this job (it will be $iUpdateInterval minutes from now)
$At = $(Get-Date) + $RepeatTimeSpan;
# Create the trigger to start this job
$UpdateTrigger = New-JobTrigger -Once -At $At -RepetitionInterval $RepeatTimeSpan -RepeatIndefinitely;
# Register the job with Windows Task scheduling system
Register-ScheduledJob -Name "RunDuckDnsUpdate" -ScriptBlock $UpdateDuckDns -Trigger $UpdateTrigger -ArgumentList @($duckdns_url);
}
$AdministratorCheck = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
$VersionCheck = ($PSVersionTable.PSVersion.Major -ge 4)
$Break = $False
# Check to see if the script is being run under adminstrator credentials, and stop if it's not.
if(!($AdministratorCheck)){
Write-Warning "You need to run this from an Administrator PowerShell prompt"
$Break = $True
}
# Check on the version of Powershell
If(!($VersionCheck)){
Write-Warning "You need to be running PowerShell version 4.0 or better"
$Break = $True
}
# Check to see if we need to exit
If($Break){
Break
}
# Clear any existing jobs
$jobs = @("RunDuckDnsUpdate", "StartDuckDnsJob")
foreach ($job in $jobs) {
If(Get-ScheduledJob $job -ErrorAction SilentlyContinue) {
Unregister-ScheduledJob $job
}
}
# Check to see if the "DuckDNS Updater" event log source already exists,
# and if it doesn't then create it
if (!([System.Diagnostics.EventLog]::SourceExists("DuckDNS Updater"))){
New-EventLog -LogName "Application" -Source "DuckDNS Updater"
}
# Set the trigger for the bootup task
$StartTrigger = New-JobTrigger -AtStartup
# Check to see if the user is super advanced and supplied their own IP address or not
if($MyIP.Length -ne 0){
# Register the job that will run when windows first starts with the Windows Task Scheduler service
Register-ScheduledJob -Name "StartDuckDnsJob" -ScriptBlock $SetupRepeatingJob -Trigger $StartTrigger -ArgumentList @($MyDomain,$MyToken,$MyUpdateInterval,$MyIP)
# Run the actual update job
& $SetupRepeatingJob $MyDomain $MyToken $MyUpdateInterval $MyIP
} else {
# Register the job that will run when windows first starts with the Windows Task Scheduler service
Register-ScheduledJob -Name "StartDuckDnsJob" -ScriptBlock $SetupRepeatingJob -Trigger $StartTrigger -ArgumentList @($MyDomain,$MyToken,$MyUpdateInterval)
# Run the actual update job
& $SetupRepeatingJob $MyDomain $MyToken $MyUpdateInterval
}
Write-Host "All done - your DuckDNS will now update automatically, and will continue to do so across system restarts."
Write-Host "Have a nice day!"