-
Notifications
You must be signed in to change notification settings - Fork 1
/
30.Configure-DNS.ps1
165 lines (144 loc) · 6.92 KB
/
30.Configure-DNS.ps1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<#
.Synopsis
.DESCRIPTION
This script configures DNS Zones, by creating DNS zones where needed, it configures Aging on the DNS zones
And it configures Server Scavenging on the DNS server that is also PDC for the domain.
It does not configure DNS forwarding.
.EXAMPLE
.\30.Configure-DNS.ps1 -XmlFile .\ADStructure_Contoso.com.xml -Verbose
.NOTES
Author : Ben van Zanten
Company: Valid
Date : Dec 2015
Version: 1.0
History: 1.0 Initial version
#>
[CmdletBinding(SupportsShouldProcess=$true,
ConfirmImpact='Medium')]
Param
(
# Name of the input file, default is: ADStructure.xml
[Parameter(Mandatory=$false,Position=1,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false)]
[ValidateScript({Test-Path $_})]
[string]$XmlFile='.\ADStructure_Contoso.com.xml',
# Name of the domain. For instance Contoso. If not given, the domain from the XML is used. The XML file supports multiple domains.
[Parameter(Mandatory=$False,Position=2)]
[string]$DomainName
)
Begin {
Import-Module .\DeployAdLib.psd1
# Test for elevation :
if (-not(Test-AdminStatus)) {
Write-Error "Run this script elevated! This script requires administrative permissions."
break
}
$domName = Get-DomainName -XmlFile $XmlFile -DomainName $DomainName
[xml]$forXML = Get-Content $XmlFile
$domXML = $forXML.forest.domains.domain | Where-Object { $_.name -eq $domName }
$dcXML = $domXML.DCs.DC | Where-Object { $_.Name -eq $Env:ComputerName }
}
Process
{
#
# Create & Configure Domain DNS zones.
#
# (Todo: onderscheid maken tussen forest en domain based zones?
#
# Set-DnsServerZoneAging -Name "$($domXML.dnsname)" -Aging $True -RefreshInterval "7.00:00:00" -NoRefreshInterval "7.00:00:00"
# Set-DnsServerZoneAging -Name "_MSDCS.$($domXML.dnsname)" -Aging $True -RefreshInterval "7.00:00:00" -NoRefreshInterval "7.00:00:00"
ForEach ($Zone in $domXML.DNS.zones.zone) {
$ZoneHT = Convert-XmlToHT $Zone
$ZoneHT.Remove("Aging")
$ZoneAgingHT = Convert-XmlToHT $Zone.Aging
$ExistingZone = Get-DnsServerZone -Name $Zone.name -ErrorAction SilentlyContinue
if ($ExistingZone) {
Write-Verbose "Zone $($Zone.name) already exists. Setting aging."
$ZoneAgingHT["name"] = $Zone.name
Set-DnsServerZoneAging @ZoneAgingHT
} else {
if ($ZoneHT.NetworkID) {
Write-Verbose "Adding zone $($Zone.NetworkID)"
$ZoneHT.Remove("name")
} else {
Write-Verbose "Adding zone $($Zone.name)"
}
$DnsRevZone = Add-DnsServerPrimaryZone @ZoneHT -PassThru -ErrorAction SilentlyContinue
if ($DnsRevZone) {
$ZoneAgingHT["name"] = $DnsRevZone.ZoneName
Set-DnsServerZoneAging @ZoneAgingHT
}
}
}
#
# Add Reverse lookup zones.. NOT done using subnets... a /25 subnet cannot be used in DNS rev lookup zone, must be converted to /24
#
# ForEach ($Subnet in $forXML.forest.subnets.subnet) {
# Write-Verbose "Adding zone $($Subnet.name)"
# $DnsRevZone = Add-DnsServerPrimaryZone -NetworkId $Subnet.name -ReplicationScope Domain -DynamicUpdate Secure -PassThru -ErrorAction SilentlyContinue
# if ($DnsRevZone) {
# Set-DnsServerZoneAging -Name $DnsRevZone.ZoneName -Aging $True -RefreshInterval "7.00:00:00" -NoRefreshInterval "7.00:00:00"
# }
# }
#
# Configure all remaining DNS zones with Aging
# Uitgezet.. de Zones hebben in de XML al Aging informatie... dan moeten ze allemaal maar in de XML komen.
#
# Get-DnsServerZone | Where-Object { $_.ZoneName -notin "0.in-addr.arpa","127.in-addr.arpa","255.in-addr.arpa" } | Set-DnsServerZoneAging -Aging $True -RefreshInterval "7.00:00:00" -NoRefreshInterval "7.00:00:00"
#
# Configure Server scavenging on the First DC (PDC FSMO) in the domain
#
if ($Env:ComputerName -eq $domXML.parameters.FSMO.PDC) {
Write-Verbose "Enabling DNS Server scavenging on the local machine..."
Set-DnsServerScavenging -ScavengingState $True -ApplyOnAllZones -ScavengingInterval "7.00:00:00"
}
Write-Host "`nCurrent Zone & Aging settings:"
Get-DnsServerZone | Where-Object { $_.ZoneType -eq 'Primary' } | Get-DnsServerZoneAging | Format-Table ZoneName,AgingEnabled,RefreshInterval,NoRefreshInterval -AutoSize
Write-Host "Current Scavenging settings:"
Get-DnsServerScavenging
#
# Configure Server (Conditional) Forwarding...
#
Write-Output "Enabling Forwarders..."
ForEach ($Forwarder in $dcXML.DNS.Forwarders) {
$ForwHT = Convert-XmlToHT $Forwarder
$ForwHT.Remove("name")
# Convert IPAddress (comma separated) to an array...
$IPs = $ForwHT["IPAddress"]
$ForwHT.Remove("IPAddress")
$ForwHT["IPAddress"]=$IPs.Split(',')
$ForwHT | FT *
Set-DnsServerForwarder @ForwHT
}
Write-Output "Enabling Conditional Forwarders..."
ForEach ($CF in $dcXML.DNS.ConditionalForwarders.ConditionalForwarder) {
$ForwHT = Convert-XmlToHT $CF
$ForwHT.Remove("#comment")
# Convert IPAddress (comma separated) to an array...
$IPs = $ForwHT["MasterServers"]
$ForwHT.Remove("MasterServers")
$ForwHT["MasterServers"]=$IPs.Split(',')
Write-Output "Enabling Conditional Forwarder with properties:"
$ForwHT | FT *
if ($ForwHT.ReplicationScope -match 'Custom') {
Try {
$DnsPart = Get-DnsServerDirectoryPartition -Name $ForwHT.DirectoryPartitionName -ErrorAction SilentlyContinue
} catch { }
if (!($DnsPart)) {
"DNS partition $($ForwHT.DirectoryPartitionName) does not exist.. creating it"
Add-DnsServerDirectoryPartition -Name "$($ForwHT.DirectoryPartitionName)"
}
if (!((Get-DnsServerDirectoryPartition -Name $ForwHT.DirectoryPartitionName ).Flags -Match 'Enlisted')) {
Register-DnsServerDirectoryPartition -Name "$($ForwHT.DirectoryPartitionName)"
}
}
Try {
$ForwarderZone = Get-DnsServerZone -Name $ForwHT.Name -ErrorAction SilentlyContinue
} catch { }
if (!($ForwarderZone)) {
Add-DnsServerConditionalForwarderZone @ForwHT
}
}
}