forked from UnklAdM/update-godaddy-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_gd_dns_template.ps1
168 lines (122 loc) · 5.59 KB
/
update_gd_dns_template.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Function Start-CountDownTimer {
Param (
[CmdletBinding()]
[Parameter(Position=0)]
[ValidateNotNullOrEmpty()]
[ValidatePattern("[0-9]*")]
[int]$Seconds,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[ValidatePattern("[0-9]*")]
[int]$Minutes,
[Parameter(Position=2)]
[ValidateNotNullOrEmpty()]
[ValidatePattern("[0-9]*")]
[int]$Hours
)
$Timer = 0
$i = 1
if (!$Seconds -and !$Minutes -and !$Hours) {
Write-Warning "No parameters were supplied, default is 1 minute"
$Timer = New-TimeSpan -Minutes 1
#Write-Error -Message "At least one parameter is required" -RecommendedAction "Usage: Start-CountDownTimer -seconds 35" -Category NotSpecified -Exception "Missing one of the following parameters -Seconds, -Minutes, -Hours" -TargetObject "Missing Parameters"
}
Write-Verbose "Following values were supplied"
Write-Verbose "`nSeconds: $Seconds`nMinutes: $Minutes `nHours: $Hours"
if ($Seconds) {
Write-Verbose "Adding $Seconds seconds to Timer"
$Timer = New-TimeSpan -Seconds $Seconds
}
if ($Minutes) {
Write-Verbose "Adding $Minutes Mintues to Timer"
$Timer += New-TimeSpan -Minutes $Minutes
}
if ($Hours) {
Write-Verbose "Adding $Hours Hours to Timer"
$Timer += New-TimeSpan -Hours $Hours
}
Write-Verbose "Total Timer Value: $Timer"
Write-Verbose "Starting the progress bar"
while ( $i -ne $Timer.TotalSeconds) {
Write-Verbose "Calculating the percentage"
$Percentage = [math]::Round($i/$Timer.TotalSeconds*100)
Write-Verbose "Percentage: $Percentage"
Write-Verbose "Calculating total time remaining"
$TimeDifference = $Timer - (New-TimeSpan -Seconds $i)
Write-Verbose "Time remaining $TimeDifference"
Write-Progress -Activity "Time Remaining: $TimeDifference" -Status "Timer Progress $Percentage%" -PercentComplete $Percentage
Write-Verbose "Incrementing `$i($i) variable"
$i++
Write-Verbose "Adding 1 second sleep"
sleep 1
}
Write-Verbose "Marking progress bar done"
Write-Progress -Activity "Time Remaining: $TimeDifference" -Status "Done" -Completed
}
<#
This script was originally forked from https://github.com/markafox/GoDaddy_Powershell_DDNS and heavily modified for
my own internal use. This works great for me. Your mileage may vary.
#>
<#
This script is used to check and update your GoDaddy DNS server to the IP address of your current internet connection.
First go to GoDaddy developer site to create a developer account and get your key and secret
https://developer.godaddy.com/getstarted
Update the first 4 varriables with your information
#>
#This data is now in the include file
$domain="xxx.com" # your domain
$type="A" # Record type A, CNAME, MX, etc.
$name="@" # name of record to update. Store number.
$ttl=600 # Time to Live min value 600
$port=1 # Required port, Min value 1
$weight=1 # Required weight, Min value 1
$key="xxxxxxxxxxxx" # key for godaddy developer API - prod
$secret="xxxxxxxxxxxx" # secret for godaddy developer API - prod
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$headers = @{}
$headers["Authorization"] = 'sso-key ' + $key + ':' + $secret
$period = [timespan]::FromSeconds(45)
$lastRunTime = [DateTime]::MinValue
echo "Start to check..."
while (1)
{
$result = Invoke-WebRequest https://api.godaddy.com/v1/domains/$domain/records/$type/$name -method get -headers $headers -UseBasicParsing
$content = ConvertFrom-Json $result.content
$dnsIp = $content.data
# Get public ip address there are several websites that can do this.
# $currentIp = Invoke-RestMethod http://ipinfo.io/json | Select-Object -ExpandProperty ip
# $currentIp = (Invoke-WebRequest "https://www.cip.cc/").content
$body = Invoke-WebRequest -Uri "https://www.cip.cc/"
$str=$null
if ($body.StatusCode -eq 200)
{
[string]$str = $body.ParsedHtml.body.innerHTML
$StartIndex = $str.IndexOf("IP : ") + 4
$EndIndex = $str.IndexOf("µØÖ· : ") - 2
$length = $EndIndex - $StartIndex - 1
$currentIp = $str.Substring($StartIndex + 1, $length)
echo "CurrentIP: $currentIp"
if ( $currentIp -ne $dnsIp ) {
$Request = @(@{data=$currentIp;port=$port;priority=0;protocol="string";service="string";ttl=$ttl;weight=$weight })
$JSON = Convertto-Json $request
$result = Invoke-WebRequest https://api.godaddy.com/v1/domains/$domain/records/$type/$name -method put -headers $headers -Body $json -ContentType "application/json" -UseBasicParsing
}
if ( $currentIp -eq $dnsIp ) {
echo "IP's are equal, no update required"
}
echo "Wait for another check..."
}
else
{
Write-Warning "Bad Request to get current ip."
}
<#
$lastRunTime = Get-Date
# If the next period isn't here yet, sleep so we don't consume CPU
while ((Get-Date) - $lastRunTime -lt $period) {
Start-Sleep -Seconds 600
}
#>
Start-CountDownTimer -Minutes 10
}
#EOF