Created
September 8, 2019 13:09
-
-
Save prajaybasu/bf31e93604a756e4f54fa38e016e782d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$subDomain = "redacted"; | |
$rootDomain = "redacted"; | |
$zoneName = $rootDomain | |
$resourceGroupName = $rootDomain | |
$secret = 'redacted'; | |
$tenantId = 'redacted'; | |
$applicationId = 'redacted'; | |
$password = ConvertTo-SecureString $secret -AsPlainText -Force | |
$credential = New-Object System.Management.Automation.PSCredential ($applicationId, $password) | |
Function Get-ExternalIP { | |
$ip = Invoke-RestMethod http://ip.zxq.co/self | Select -exp ip | |
Return $ip | |
} | |
$domain = $subDomain+ "." + $rootDomain; | |
$DNSExternalIP = ((Resolve-DnsName -Name $domain -Type A -DnsOnly)[0]).IPAddress; | |
$CurrentIP = Get-ExternalIP | |
If ($CurrentIP -ne $DNSExternalIP) { | |
Connect-AzAccount -ServicePrincipal -Credential $credential -Tenant $tenantId | |
$aRecords = @() | |
$aRecords += New-AzDnsRecordConfig -IPv4Address $CurrentIP | |
New-AzDnsRecordSet -Name $subDomain -RecordType A -ZoneName $zoneName -ResourceGroupName $resourceGroupName -Ttl 3600 -DnsRecords $aRecords -Overwrite | |
$message = "Updated IP for $domain to $CurrentIP successfully"; | |
Write-EventLog –LogName "Application" –Source “Azure Dynamic DNS” –EntryType Information –EventID 1 –Message $message | |
} | |
Else | |
{ | |
$message = "DNS record matches the current IP, $CurrentIP - Did not update record"; | |
Write-EventLog –LogName "Application" –Source “Azure Dynamic DNS” –EntryType Information –EventID 2 –Message $message | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment