Created
September 3, 2020 17:30
-
-
Save machv/d7a10f83fa1f40e09347ced50a034ec1 to your computer and use it in GitHub Desktop.
Azure Automation to Sync DNS resolution to Route Table
This file contains hidden or 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
Param( | |
[Parameter (Mandatory = $true)] | |
[sring]$DnsName, | |
[string]$RouteName | |
) | |
if($RouteName -eq "") { | |
$RouteName = $DnsName | |
} | |
#region Azure Automation bootstrap | |
try | |
{ | |
$servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" | |
"Logging in to Azure..." | |
Connect-AzAccount ` | |
-ServicePrincipal ` | |
-TenantId $servicePrincipalConnection.TenantId ` | |
-ApplicationId $servicePrincipalConnection.ApplicationId ` | |
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | |
} | |
catch { | |
if (!$servicePrincipalConnection) | |
{ | |
$ErrorMessage = "Connection AzureRunAsConnection not found." | |
throw $ErrorMessage | |
} else{ | |
Write-Error -Message $_.Exception | |
throw $_.Exception | |
} | |
} | |
$smtpCredential = Get-AutomationPSCredential -Name "SendGridCredentials" | |
#endregion | |
#region Configuration | |
# Azure | |
$routeTableResourceGroupName = "test_group" | |
$routeTableName = "RT-ForcedTunneling" | |
# Mail notifications | |
$smtpServer = "smtp.sendgrid.net" | |
$emailFrom = "Azure Automations <[email protected]>" | |
$emailTo = "[email protected]" # CC added to every e-mail sent by this script | |
#endregion | |
$result = [system.net.dns]::GetHostByName($dnsName) | |
$record = $result.AddressList | Select-Object -First 1 | |
if(-not $record) { | |
throw "Unable to resolve IP for $dnsName" | |
} | |
$description = "" | |
$prefix = "$($record)/32" | |
$routeTable = Get-AzRouteTable -ResourceGroupName $routeTableResourceGroupName -Name $routeTableName | |
$route = $routeTable.Routes | Where-Object Name -eq $routeName | |
if(-not $route) { | |
Add-AzRouteConfig -RouteTable $routeTable -Name $routeName -AddressPrefix $prefix -NextHopType "Internet" | Set-AzRouteTable | Out-Null | |
$description = "New route entry created in $routeTableName (RG $routeTableResourceGroupName) with address prefix $prefix." | |
} else { | |
if($prefix -ne $route.AddressPrefix) { | |
Set-AzRouteConfig -RouteTable $routeTable -Name $routeName -AddressPrefix $prefix -NextHopType Internet | Set-AzRouteTable | Out-Null | |
$description = "Prefix of route $routeName in $routeTableName (RG $routeTableResourceGroupName) has been changed from $($route.AddressPrefix) to $prefix." | |
} | |
} | |
$description | |
if($description -ne "") { | |
Send-MailMessage -SmtpServer $smtpServer -Credential $smtpCredential -UseSsl -Port 587 ` | |
-From $emailFrom -To $emailTo -Subject "Updated Routing Table $($routeTableName)" ` | |
-Body $description -Encoding UTF8 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment