windows:scripting:dns_ps_bulk

PowerShell: Create multiple DNS records using a script and a text file list

test01-cname;arecord.example.com.
test01-v4;10.0.0.1
test01-v6;ff::ff
param (
    [Parameter(Mandatory=$true,Position=1)][string]$DnsServer,
    [Parameter(Mandatory=$true,Position=2)][string]$DnsZone,
    [Parameter(Mandatory=$true,Position=3)][string]$DnsFile
)
Clear-Host

Function MoveCursorToEnd {
	$CurrentPosition = $host.UI.RawUI.CursorPosition
	$WindowSize = $Host.UI.RawUI.WindowSize
	$CurrentPosition.X = $WindowSize.Width - 8
	$host.UI.RawUI.CursorPosition = $CurrentPosition
}


Write-Host -NoNewLine "Server .... : " $DnsServer
MoveCursorToEnd 
If (Test-Connection $DnsServer) {
	Write-Host "[  OK  ]" -foregroundcolor "green"
	$ServerOk = $TRUE;
} Else {
	Write-Host "[ FAIL ]" -foregroundcolor "red"
	$ServerOk = $FALSE;
}


Write-Host -NoNewLine "Zone ...... : " $DnsZone
MoveCursorToEnd
If (($ServerOk -eq $TRUE) -and ($(Get-DnsServerZone -ComputerName $DnsServer -Name $DnsZone) -ne $FALSE)) {
	Write-Host "[  OK  ]" -foregroundcolor "green"
	$ZoneOk = $TRUE
} Else {
	Write-Host "[ FAIL ]" -foregroundcolor "red"
	$ZoneOk = $FALSE
}

Write-Host -NoNewLine "File ...... : " $DnsFile
MoveCursorToEnd 
If (Test-Path $DnsFile) {
	Write-Host "[  OK  ]" -foregroundcolor "green"
	$FileOk = $TRUE
} Else {
	Write-Host "[ FAIL ]" -foregroundcolor "red"
	$FileOk = $FALSE
}

Write-Host " "

$RegExIPv4 = "^([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
$RegExIPv6 = "^(((([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}))|(([0-9a-fA-F]{1,4}:){1,7}:)|(([0-9a-fA-F]{1,4}:){1,6}(:[0-9a-fA-F]{1,4}){1})|(([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){2})|(([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){3})|(([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){4})|(([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){5})|(([0-9a-fA-F]{1,4}:){1}(:[0-9a-fA-F]{1,4}){6})|(:(:[0-9a-fA-F]{1,4}){1,7})|(([0-9a-fA-F]{1,4}){1}:(:[0-9a-fA-F]{1,4}){1,6})|(([0-9a-fA-F]{1,4}:){2}(:[0-9a-fA-F]{1,4}){1,5})|(([0-9a-fA-F]{1,4}:){3}(:[0-9a-fA-F]{1,4}){1,4})|(([0-9a-fA-F]{1,4}:){4}(:[0-9a-fA-F]{1,4}){1,3})|(([0-9a-fA-F]{1,4}:){5}(:[0-9a-fA-F]{1,4}){1,2})|(([0-9a-fA-F]{1,4}:){6}(:[0-9a-fA-F]{1,4}){1})|(::)|(fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,})|(::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))|(([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))))$"
$RegExcName = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.$"
If (($ServerOk -eq $TRUE) -and ($ZoneOk -eq $TRUE) -and ($FileOk -eq $TRUE)) {
	ForEach ($record in Get-Content $DnsFile) {
		$cols = $record -split ";"
		$RecordType=""
		If ($cols[1] -match $RegExIPv4) {
			Write-Host -NoNewLine "A          => " $cols[0]
			MoveCursorToEnd
			If ($(Add-DnsServerResourceRecordA -ComputerName $DnsServer -ZoneName $DnsZone -Name $cols[0] -IPv4Address $cols[1]) -ne $FALSE) {Write-Host "[  OK  ]" -foregroundcolor "green"} Else {Write-Host "[ FAIL ]" -foregroundcolor "red"}
		}
		If ($cols[1] -match $RegExIPv6) {
			Write-Host -NoNewLine "AAAA       => " $cols[0]
			MoveCursorToEnd
			If ($(Add-DnsServerResourceRecordAAAA -ComputerName $DnsServer -ZoneName $DnsZone -Name $cols[0] -IPv6Address $cols[1]) -ne $FALSE) {Write-Host "[  OK  ]" -foregroundcolor "green"} Else {Write-Host "[ FAIL ]" -foregroundcolor "red"}
		}
		If ($cols[1] -match $RegExcName) {
			Write-Host -NoNewLine "CNAME      => " $cols[0]
			MoveCursorToEnd
			If ($(add-dnsserverresourcerecordcname -ComputerName $DnsServer -ZoneName $DnsZone -Name $cols[0] -HostNameAlias $cols[1]) -ne $FALSE) {Write-Host "[  OK  ]" -foregroundcolor "green"} Else {Write-Host "[ FAIL ]" -foregroundcolor "red"}
		}
	}
} Else {
	Write-Host "ERRORS!!"
}
.\dns.ps1 -DnsServer ad01 -DnsZone example.com -DnsFile dns.txt
  • windows/scripting/dns_ps_bulk.txt
  • Last modified: 2019/10/31 09:06
  • by 127.0.0.1