This document describes a PowerShell script designed to import DNS records into the qa.master.internal subdomain of the master.internal DNS zone.
The script automates the process of adding or updating DNS records in the qa.master.internal subdomain. It's particularly useful when you need to import a large number of DNS records from a text file.
The script performs the following tasks:
qa.master.internal.master.internal DNS zoneSave the script to a file named Import-QASubdomainRecords.ps1.
Prepare your input file with DNS records. Each line should follow this format:
name IN recordType data
For example:
server1 IN A 192.168.1.10
www IN CNAME server1.qa.master.internal.
Edit the script to set the correct file path:
$filePath = "C:\path\to\your\dns_file.txt" # Replace with the actual path to your file
Open PowerShell as an administrator.
Navigate to the directory containing the script.
Run the script:
.\Import-QASubdomainRecords.ps1
Review the output to ensure all records were added or updated successfully.
Add-Or-Update-DNSRecord: Adds or updates a single DNS record.Clean-DomainName: Formats domain names correctly for the qa.master.internal subdomain.$zoneName: Set to "master.internal"$subdomainPrefix: Set to "qa"If you encounter issues:
# Function to add or update DNS records
function Add-Or-Update-DNSRecord {
param (
[string]$ZoneName,
[string]$Name,
[string]$Type,
[string]$Data
)
try {
# Construct the full name for the record
$fullName = if ($Name -eq "@") { $ZoneName } else { "$Name.$ZoneName" }
# First, try to delete the existing record
$deleteCmd = "dnscmd /RecordDelete $ZoneName $Name $Type /f"
Invoke-Expression $deleteCmd | Out-Null
# Now, add the new record
$addCmd = "dnscmd /RecordAdd $ZoneName $Name $Type $Data"
$result = Invoke-Expression $addCmd
if ($result -match "Command completed successfully") {
Write-Host "Successfully added/updated $Type record for $fullName" -ForegroundColor Green
} else {
Write-Host "Error adding/updating $Type record for $fullName. Result: $result" -ForegroundColor Red
}
}
catch {
Write-Host "Exception when adding/updating $Type record for $fullName`: $_" -ForegroundColor Red
}
}
# Function to clean up domain names
function Clean-DomainName {
param (
[string]$Name,
[string]$ZoneName,
[string]$SubdomainPrefix
)
# Remove the zone name and any trailing dots
$cleanName = $Name -replace "\.$ZoneName\.$", "" -replace "\.$", ""
# If the name is exactly the subdomain prefix, return "@"
if ($cleanName -eq $SubdomainPrefix) {
return "@"
}
# If the name doesn't start with the subdomain prefix, add it
if (-not $cleanName.StartsWith("$SubdomainPrefix.")) {
$cleanName = "$SubdomainPrefix.$cleanName"
}
# Remove any double dots
$cleanName = $cleanName -replace "\.{2,}", "."
return $cleanName
}
# Set the zone name and file path
$zoneName = "master.internal"
$subdomainPrefix = "qa"
$filePath = "C:\qa.master.internal.dns" # Replace with the actual path to your file
# Counter for processed records
$recordCount = 0
# Process the file and add/update records
Get-Content $filePath | ForEach-Object {
if ($_ -match '^(\S+)\s+IN\s+(\S+)\s+(.+)$') {
$name = $matches[1]
$type = $matches[2]
$data = $matches[3]
# Clean up the name
$name = Clean-DomainName -Name $name -ZoneName $zoneName -SubdomainPrefix $subdomainPrefix
Add-Or-Update-DNSRecord -ZoneName $zoneName -Name $name -Type $type -Data $data
$recordCount++
}
}
# Print summary
Write-Host "Import complete. Processed $recordCount records." -ForegroundColor Cyan
# Optionally, you can add a pause at the end to keep the PowerShell window open
Read-Host -Prompt "Press Enter to exit"
Always backup your DNS configuration before running this script, especially in a production environment.
If you encounter any issues or need further assistance, please contact the IT support team.
Last updated: [Current Date]