This script is very handy when creating multiple bulk DNS records in the Microsoft DNS server. It is very plain instructions how to use. At the core of this script Add-DnsServerResourceRecordA is the PowerShell cmdlet which is doing the task of creating DNS records.
To use this script create a csv file with headers hostname, ip, zonename. In my case all my records are hardcoded within powershell script added as an array. If you have separate csv file replace line from 1 to 11 with command Import-Csv filelocation\records.csv.
Note: Make sure you have installed RSAT tools, which will install ActiveDirectory modules in the PowerShell, which is prerequisite.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$dnsRecords = @() $dnsRecords += 'hostname,ip,zonename' $dnsRecords += 'k8slb,192.168.34.60,vcloud-lab.com' $dnsRecords += 'k8smaster01,192.168.34.61,vcloud-lab.com' $dnsRecords += 'k8smaster02,192.168.34.62,vcloud-lab.com' $dnsRecords += 'k8smaster03,192.168.34.63,vcloud-lab.com' $dnsRecords += 'k8sworker01,192.168.34.66,vcloud-lab.com' $dnsRecords += 'k8sworker02,192.168.34.67,vcloud-lab.com' $dnsRecords += 'k8sworker03,192.168.34.68,vcloud-lab.com' $dnsRecords = $dnsRecords | ConvertFrom-Csv foreach ($dnsRecord in $dnsRecords) { $ip = $dnsRecord.ip.Split('.') [array]::Reverse($ip) $ptrZoneName = "{0}.{1}.{2}.in-addr.arpa" -f $ip[1], $ip[2], $ip[3] #Write-Host "Adding record hostname '$($dnsRecord.hostname)' - $($dnsRecord.ip) " Add-DnsServerResourceRecordA -Name $dnsRecord.hostname -IPv4Address $dnsRecord.ip -ZoneName $dnsRecord.zonename -CreatePtr -AllowUpdateAny $recordA = Get-DnsServerResourceRecord -Name $dnsRecord.hostname -ZoneName $dnsRecord.zonename -RRType A $recordPtr = Get-DnsServerResourceRecord -Name $ip[0] -ZoneName $ptrZoneName -RRType Ptr [PSCustomObject]@{ ForwardZoneName = $dnsRecord.zonename RecordA_Name = $recordA.HostName RecordA_IP = $recordA.RecordData.IPv4Address BackwardZoneName = $ptrZoneName RecordPtr_Name = $recordPtr.HostName RecordPtr_HostName = $recordPtr.RecordData.PtrDomainName } } |
After running script I see below results on the PowerShell console.
Download this script here New-DnsRecords.ps1, This script is also available on github.com/janviudapi.
You can verify the same on DNS Manager MMC, Multiple DNS A records under Forward lookup zones and Ptr records under Reverse lookup zones are created successfully.
Useful Articles
POWERSHELL: INSTALLING AND CONFIGURING ACTIVE DIRECTORY
POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE (CHANGE) MANAGER NAME IN ORGANIZATION TAB OF USER
POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE PROXYADDRESSES IN USER PROPERTIES ATTRIBUTE EDITOR
Add multiple proxy addresses with Microsoft PowerShell in Active Directory Groups
Creating a password reset tool with PowerShell GUI
Powershell one liner: Create multiple user accounts
Active Directory Powershell: Create bulk users from CSV file
Active Directory Powershell: Aduser A value for the attribute was not in the acceptable range of values
Powershell Active Directory: ADGroup Managedby - Checkbox Manager can update membership list