Last active
February 15, 2024 23:20
-
-
Save Romiko/a05adc1c9d69433bab4de536e1915db5 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
<# | |
.SYNOPSIS | |
This script migrates DNS settings from an old Azure Public IP to a new Azure Public IP and cleans up old resources. | |
.DESCRIPTION | |
The script performs several operations in Azure: | |
1. It prompts the user for confirmation before proceeding, due to potential brief connectivity downtime. | |
2. Retrieves the DNS settings (Domain Name Label and FQDN) from the old Public IP. | |
3. Applies these DNS settings to the new Public IP. | |
4. Removes the old Load Balancer and Public IP after confirming with the user. | |
The script uses the Az PowerShell module and requires the user to have appropriate permissions to manage resources in Azure. | |
.PARAMETERS | |
- subscriptionName: The name of the Azure subscription. | |
- oldLoadBalancerName: The name of the old Load Balancer to remove. | |
- resourceGroupName: The name of the Azure resource group containing the resources. | |
- oldPublicIpName: The name of the old Public IP address to migrate from. | |
- newPublicIpName: The name of the new Public IP address to migrate to. | |
.PREREQUISITES | |
- Az PowerShell module installed. | |
- User must be logged in to Azure with sufficient permissions to manage the specified resources. | |
.EXAMPLE | |
.\MigrateAzureDNS.ps1 -subscriptionName "MySubscription" -oldLoadBalancerName "OldLoadBalancer" -resourceGroupName "MyResourceGroup" -oldPublicIpName "OldPublicIP" -newPublicIpName "NewPublicIP" | |
.NOTES | |
Author: Romiko Derbynew | |
Date: 16-02-2024 | |
#> | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$subscriptionName, | |
[Parameter(Mandatory = $true)] | |
[string]$oldLoadBalancerName, | |
[Parameter(Mandatory = $true)] | |
[string]$resourceGroupName, | |
[Parameter(Mandatory = $true)] | |
[string]$oldPublicIpName, | |
[Parameter(Mandatory = $true)] | |
[string]$newPublicIpName | |
) | |
# Check if Az.Network module is installed; install and import if necessary | |
if (-not (Get-Module -ListAvailable -Name Az.Network)) { | |
Install-Module Az.Network -Force | |
} | |
Import-Module Az.Network -Force | |
$ErrorActionPreference = "Stop" | |
Connect-AzAccount -Subscription $subscriptionName | |
Write-Host "WARNING: This operation may cause brief connectivity downtime. Do you want to continue? (Y/N)" | |
$confirmation = Read-Host | |
if ($confirmation -ne 'Y') { | |
Write-Host "Operation cancelled by the user." | |
exit | |
} | |
try { | |
$oldPrimaryPublicIP = Get-AzPublicIpAddress -Name $oldPublicIpName -ResourceGroupName $resourceGroupName | |
$primaryDNSName = $oldPrimaryPublicIP.DnsSettings.DomainNameLabel | |
$primaryDNSFqdn = $oldPrimaryPublicIP.DnsSettings.Fqdn | |
if (![string]::IsNullOrEmpty($primaryDNSName) -and ![string]::IsNullOrEmpty($primaryDNSFqdn)) { | |
Write-Host "Found the Primary DNS Name: $primaryDNSName" | |
Write-Host "Found the Primary DNS FQDN: $primaryDNSFqdn" | |
} else { | |
Write-Error "DNS settings not found for Old IP: $oldPublicIpName" | |
exit | |
} | |
Write-Host "Moving Azure DNS Names to the new Public IP" | |
$newPublicIP = Get-AzPublicIpAddress -Name $newPublicIpName -ResourceGroupName $resourceGroupName | |
$newPublicIP.DnsSettings.DomainNameLabel = $primaryDNSName | |
$newPublicIP.DnsSettings.Fqdn = $primaryDNSFqdn | |
Set-AzPublicIpAddress -PublicIpAddress $newPublicIP | |
Write-Host "DNS transfer to new Public IP completed." | |
Write-Host "Removing old Load Balancer and Public IP. Are you sure? (Y/N)" | |
$confirmation = Read-Host | |
if ($confirmation -ne 'Y') { | |
Write-Host "Operation cancelled by the user." | |
exit | |
} | |
Remove-AzLoadBalancer -Name $oldLoadBalancerName -ResourceGroupName $resourceGroupName -Force | |
Remove-AzPublicIpAddress -Name $oldPublicIpName -ResourceGroupName $resourceGroupName -Force | |
Write-Host "Cleanup of old resources completed." | |
} catch { | |
Write-Error "An error occurred: $_" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment