Differences
This shows you the differences between two versions of the page.
— | windows:scripting:dns_ps_bulk [2019/10/31 09:06] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== PowerShell: Create multiple DNS records using a script and a text file list ====== | ||
+ | ===== Text file format ===== | ||
+ | < | ||
+ | test01-cname; | ||
+ | test01-v4; | ||
+ | test01-v6; | ||
+ | </ | ||
+ | ===== Script ===== | ||
+ | < | ||
+ | param ( | ||
+ | [Parameter(Mandatory=$true, | ||
+ | [Parameter(Mandatory=$true, | ||
+ | [Parameter(Mandatory=$true, | ||
+ | ) | ||
+ | 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 " | ||
+ | MoveCursorToEnd | ||
+ | If (Test-Connection $DnsServer) { | ||
+ | Write-Host " | ||
+ | $ServerOk = $TRUE; | ||
+ | } Else { | ||
+ | Write-Host "[ FAIL ]" -foregroundcolor " | ||
+ | $ServerOk = $FALSE; | ||
+ | } | ||
+ | |||
+ | |||
+ | Write-Host -NoNewLine "Zone ...... : " $DnsZone | ||
+ | MoveCursorToEnd | ||
+ | If (($ServerOk -eq $TRUE) -and ($(Get-DnsServerZone -ComputerName $DnsServer -Name $DnsZone) -ne $FALSE)) { | ||
+ | Write-Host " | ||
+ | $ZoneOk = $TRUE | ||
+ | } Else { | ||
+ | Write-Host "[ FAIL ]" -foregroundcolor " | ||
+ | $ZoneOk = $FALSE | ||
+ | } | ||
+ | |||
+ | Write-Host -NoNewLine "File ...... : " $DnsFile | ||
+ | MoveCursorToEnd | ||
+ | If (Test-Path $DnsFile) { | ||
+ | Write-Host " | ||
+ | $FileOk = $TRUE | ||
+ | } Else { | ||
+ | Write-Host "[ FAIL ]" -foregroundcolor " | ||
+ | $FileOk = $FALSE | ||
+ | } | ||
+ | |||
+ | Write-Host " " | ||
+ | |||
+ | $RegExIPv4 = " | ||
+ | $RegExIPv6 = " | ||
+ | $RegExcName = " | ||
+ | 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 " | ||
+ | MoveCursorToEnd | ||
+ | If ($(Add-DnsServerResourceRecordA -ComputerName $DnsServer -ZoneName $DnsZone -Name $cols[0] -IPv4Address $cols[1]) -ne $FALSE) {Write-Host " | ||
+ | } | ||
+ | If ($cols[1] -match $RegExIPv6) { | ||
+ | Write-Host -NoNewLine " | ||
+ | MoveCursorToEnd | ||
+ | If ($(Add-DnsServerResourceRecordAAAA -ComputerName $DnsServer -ZoneName $DnsZone -Name $cols[0] -IPv6Address $cols[1]) -ne $FALSE) {Write-Host " | ||
+ | } | ||
+ | If ($cols[1] -match $RegExcName) { | ||
+ | Write-Host -NoNewLine " | ||
+ | MoveCursorToEnd | ||
+ | If ($(add-dnsserverresourcerecordcname -ComputerName $DnsServer -ZoneName $DnsZone -Name $cols[0] -HostNameAlias $cols[1]) -ne $FALSE) {Write-Host " | ||
+ | } | ||
+ | } | ||
+ | } Else { | ||
+ | Write-Host " | ||
+ | } | ||
+ | </ | ||
+ | ===== Usage ===== | ||
+ | < | ||
+ | .\dns.ps1 -DnsServer ad01 -DnsZone example.com -DnsFile dns.txt | ||
+ | </ | ||