-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleCheckForDnsUpdate.ps1
48 lines (40 loc) · 1.75 KB
/
ExampleCheckForDnsUpdate.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
# Import PSLinodeDnsClient module.
# Make sure you have already installed the PSFramework module as it is required for logging
Import-Module "$PSScriptRoot\PSLinodeDnsClient"
try {
# Query ipinfo.io for our public IP
$ipinfo = Invoke-WebRequest -Uri "http://ipinfo.io/json" -ErrorAction Stop
}
catch {
Stop-PSFFunction -Message "http://ipinfo.io/json - returned a '$($ipinfo.StatusCode)' code. Expected 200" -Target $ipinfo -ErrorRecord $_ -EnableException $true
return
}
try {
# Parse the IP from the response
$publicIP = ($ipinfo.Content | ConvertFrom-Json).ip
}
catch {
Stop-PSFFunction -Message "`$ipinfo.Content' was malformed" -Target $ipinfo -ErrorRecord $_ -EnableException $true
return
}
# Get list of DNS's using Get-DomainList, excluding the ones from the exclude list
$domainList = Get-DomainRecordsList $Global:config.DomainId | Where-Object {
-not $global:config.Exclude.Contains($_.Name) -and $_.type -eq "A" `
-and $_.target -ne $publicIP
}
if($domainList.Count -gt 0){
Write-PSFMessage -Level Debug -Message "Filtered Get-DomainRecordsList $($Global:config.DomainId) returned $($domainList.Count)"
}
# Go through each returned record and attempt to update the DNS
$domainList | ForEach-Object {
Write-PSFMessage -Level Debug -Message "Updating '$($_.name)' from '$($_.target)' to '$publicIP'"
# Update the target property with the new public IP
$_.target = $publicIP
# Update the domain record with the new public IP
$result = $_ | Update-DomainRecord $Global:config.DomainId
if($result){
Write-PSFMessage -Level Debug -Message "Update successful for record $($_.id)" -Target $result
continue
}
Stop-PSFFunction -Message "Update failed for record $($_.id)" -Target $_
}