Created
November 10, 2022 18:26
-
-
Save JustinGrote/4102607bd2ce2565b6125b9004ed226b to your computer and use it in GitHub Desktop.
Copy all A records from one Private DNS zone to another (or multiple)
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
using namespace Microsoft.Azure.Commands.PrivateDns.Models | |
filter Sync-AzPrivateDnsZone { | |
[CmdletBinding(SupportsShouldProcess = $true)] | |
param( | |
#The zone to copy A records from | |
[Parameter(Mandatory)][PSPrivateDNSZone]$SyncFrom, | |
#The zone to copy A records into | |
[Parameter(Mandatory, ValueFromPipeline)][PSPrivateDNSZone]$SyncTo, | |
#How fast to create records | |
[int]$ThrottleLimit = 10 | |
) | |
$ErrorActionPreference = 'Stop' | |
$srcRecords = $SyncFrom | Get-AzPrivateDnsRecordSet -RecordType 'A' | |
$dstRecords = $SyncTo | Get-AzPrivateDnsRecordSet -RecordType 'A' | |
$recordsToCopy = $srcRecords | Where-Object Name -NotIn $dstRecords.Name | |
$recordActionParams = $recordsToCopy | Foreach-Object { | |
$newRecordName = $PSItem.Name | |
$newRecords = $PSItem.Records | |
if (-not $PSCmdlet.ShouldProcess($SyncTo.Name, "Sync Record $newRecordName - $newRecords from $($SyncFrom.Name)")) {return} | |
#This is a set of parameters we will splat to the New-AzPrivateDnsRecordSet | |
return @{ | |
Name = $newRecordName | |
RecordType = 'A' | |
ResourceGroup = $SyncTo.ResourceGroupName | |
ZoneName = $SyncTo.Name | |
Ttl = 10 | |
PrivateDnsRecords = $PSItem.Records | |
} | |
} | |
$recordActionParams | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel { | |
New-AzPrivateDnsRecordSet @PSItem | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment