function Installdns
{
Servermanagercmd-q c:\role.xml| Out-null
$xml =[xml] (get-content c:\role.xml)
$dnsrole = $xml. Servermanagerconfigurationquery.role|where{$_.id-eq "DNS"}
if ($dnsrole. Installed-eq $false)
{
Servermanagercmd-i $dnsrole. Id
}
Else{write-host "The DNS server role is already installed!"}
$dns =get-service DNS
if ($dns. Status-ne "Running")
{
Start-service DNS
}
}
function SetIP
{
$nic =gwmi win32_networkadapterconfiguration-filter "ipenabled= $true"
if ($nic. dhcpenabled)
{
Write-host "Set Now" $nic. Description
$ip = "192.168.10.10"
$mask = "255.255.255.0"
$dns = "127.0.0.1"
$nic. EnableStatic ($ip, $mask)
$nic. SetDNSServerSearchOrder ($dns)
}
}
function DnsConfig
{
$zonename = "Benet.com"
$Aname = "www" #主机名称
$Aip = "192.168.10.12" #主机IP地址
$Aip _bj= "192.168.10.32"
$Cname = "FTP"
$zonename _bj= "Bj.benet.com"
$ns = "Server01.benet.com"
$nsip = "192.168.10.20"
$forwarder = "Server02.benet.com"
$forwarderip = "192.168.10.30"
Write-host "Set forward search area ... "
Start-sleep 2
Dnscmd/zoneadd $zonename/primary/file Benet.dns #创建正向查找区域
$reversip = "10.168.192" #设置反向查找区域网络ID
$reverszone = $reversip + ". In-addr.arpa"
Write-host "Set Reverse lookup zone ... "
Start-sleep 2
Dnscmd/zoneadd $reverszone/primary #创建反向查找区域
Write-host "Add host Record"
Start-sleep 2
Dnscmd/recordadd $zonename $Aname A $Aip #设置www主机IP
Dnscmd/recordadd $reverszone $Aip. Split (".") [3] PTR $Aname "." $zonename #设置www主机反向查询名称
Write-host "Setting aliases"
Start-sleep 2
Dnscmd/recordadd $zonename $Cname Cname $Aname "." $zonename #设置www主机别名为ftp
Write-host "Setting up benet.com subdomain BJ"
Start-sleep 2
Dnscmd/zoneadd $zonename _bj/primary
Write-host "adding host records in Bj.benet.com"
Start-sleep 2
Dnscmd/recordadd $zonename _bj $Aname A $Aip _bj #在子域bj. betnet.com Add www host records
Dnscmd/recordadd $reverszone $Aip _bj.split (".") [3] PTR $Aname "." Add a reverse query $zonename _bj #在子域bj. benet.com
Write-host "setting host aliases in Bj.benet.com"
Start-sleep 2
Dnscmd/recordadd $zonename _bj $Cname Cname $Aname "." $zonename _bj #设置www. bj.benet.com Host alias is FTP
Write-host "Add Name server Server01"
Start-sleep 2
Dnscmd/recordadd $zonename $ns. TrimEnd ("benet.com") A $nsip #添加server01的主机记录
Dnscmd/recordadd $reverszone $nsip. Split (".") [3] PTR $ns #添加server01的反向查询记录
Dnscmd/recordadd $zonename "@" NS $ns #将server01设置为名称服务器
Write-host "Set delegation to sh.benet.com to Server01"
Start-sleep 2
Dnscmd/recordadd $zonename sh NS $ns
Write-host "Setting the DNS service forwarder to Server02"
Start-sleep 2
Dnscmd/recordadd $zonename $forwarder. TrimEnd ("benet.com") A $forwarderip
Dnscmd/recordadd $reverszone $forwarderip. Split (".") [3] PTR $forwarder
Dnscmd/resetforwarders $forwarderip/timeout 5/slave #参数Slave表示如果转发器未能完成查询, no iterative query,/noslave enable iterative query
}
Installdns
SetIP
DnsConfig
PowerShell Script-dns