#=====================================================================================================================================
#
# NAME: Update_DNSRecord_v1.0.ps1
#
# AUTHOR: Florent APPOINTAIRE
# DATE: 11/02/2020
# VERSION: 1.0
#
# COMMENT: The purpose of this script is to update your Azure DNS public IP for @ with a dynamic public IP
# USING : Update values with you information
#
#=====================================================================================================================================
#Subscription Id
$subscriptionId = ""
#DynDns link
$DynDNS = ""
#Resource group where S2SVpn is stored
$RGName = "DNS"
#DNS names to change
$dnsNames = "url1.be","url2.be"
#Connect to the subscription
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$connectionResult = Connect-AzAccount -Tenant $servicePrincipalConnection.TenantID `
-ApplicationId $servicePrincipalConnection.ApplicationID `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint `
-ServicePrincipal
Select-AzSubscription -SubscriptionId $subscriptionId
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
#Get the public IP of your Dynamic DNS
$IP = ([System.Net.DNS]::GetHostAddresses($DynDNS)).IPAddressToString
foreach ($dnsName in $dnsNames){
#Get the IP in your Azure DNS
$rs = Get-AzDnsRecordSet -name "@" -RecordType A -ZoneName $dnsName -ResourceGroupName $RGName
$rs = $rs.Records[0].Ipv4Address
Write-Output "--------------------------"
Write-Output "DNS Zone : $dnsName"
Write-Output "Public IP : $IP"
Write-Output "Azure DNS IP : $rs"
#Check if your public IP and the Azure Public IP are the same
if($IP -ne $rs)
{
#IP Changed, we need to update
Write-Output "IP Update In Progress..."
$rs = Get-AzDnsRecordSet -name "@" -RecordType A -ZoneName $dnsName -ResourceGroupName $RGName
$rs.Records[0].Ipv4Address = $IP
Set-AzDnsRecordSet -RecordSet $rs
} else {
#IP didn't change, nothing to do
Write-Output "IP Already Up To Date"
}
}