Windows DNS服务通过Powershell管理DNS A/CNAME记录
#A新增
Add-DnsServerResourceRecordA -Name "host23" -ZoneName "lianlianpay.com" -AllowUpdateAny -IPv4Address "172.18.99.23" -TimeToLive 01:00:00
#A查询
Get-DnsServerResourceRecord -ZoneName "lianlianpay.com" -Name "host23" -RRType "A"
#A删除
Remove-DnsServerResourceRecord -ZoneName "lianlianpay.com" -RRType "A" -Name "Host23" -RecordData "172.18.99.23"
##修改A记录 host23.lianlianpay.com 的IP指向为172.0.0.23
$ARecord = Get-WmiObject -Namespace root\MicrosoftDNS -class microsoftdns_atype | ? {$_.OwnerName -eq "host23.lianlianpay.com"}
$ARecord.Modify($ARecord.TTL,"127.0.0.223")
#修改DNS A记录
$ZoneName = "lianlianpay.com"
$HostName = "host23"
$As = Get-DnsServerResourceRecord -ZoneName $ZoneName | ? {$_.HostName -eq $HostName} |sort RecordData
$NewRecord = $As.Clone()
$NewRecord.RecordData.IPv4Address ="1.1.1.1"
Set-DnsServerResourceRecord -ZoneName $ZoneName -OldInputObject $As -NewInputObject $NewRecord
#或者一条命令实现
$ZoneName = 'lianlianpay.com'; $HostName = 'host23'; $As = Get-DnsServerResourceRecord -ZoneName $ZoneName | ? {$_.HostName -eq $HostName} |sort RecordData; $NewRecord = $As.Clone(); $NewRecord.RecordData.IPv4Address ='1.1.1.3'; Set-DnsServerResourceRecord -ZoneName $ZoneName -OldInputObject $As -NewInputObject $NewRecord
#查询域名记录 CNAME
Get-DnsServerResourceRecord -ZoneName 'lianlianpay-inc.com' -Name 'test' -RRType "CNAME"
#删除域名记录 CNAME
Remove-DnsServerResourceRecord -ZoneName 'lianlianpay-inc.com' -RRType "CNAME" -Name 'test'
#添加CNAME示例
Add-DnsServerResourceRecordCName -Name "test" -HostNameAlias "hzt4.lianlianpay-inc.com" -ZoneName "lianlianpay-inc.com"
##修改CNAME记录,将test.lianlianpay.com的CNAME hzt1.lianlianpay.com修改为hzt4.lianlianpay.com
$ZoneName = "lianlianpay.com"
$HostName = "test"
$As = Get-DnsServerResourceRecord -ZoneName $ZoneName | ? {$_.HostName -eq $HostName} |sort RecordData
$NewRecord = $As.Clone()
$NewRecord.RecordData.HostNameAlias ="hzt4.lianlianpay.com"
Set-DnsServerResourceRecord -ZoneName $ZoneName -OldInputObject $As -NewInputObject $NewRecord
#或者一条命令实现
$ZoneName = 'lianlianpay.com'; $HostName = 'test'; $As = Get-DnsServerResourceRecord -ZoneName $ZoneName | ? {$_.HostName -eq $HostName} |sort RecordData; $NewRecord = $As.Clone(); $NewRecord.RecordData. HostNameAlias ='hzt4.lianlianpay.com'; Set-DnsServerResourceRecord -ZoneName $ZoneName -OldInputObject $As -NewInputObject $NewRecord
#批量增加DNS A记录,采用字典
$aname = @{
"dev-mqnameserver01" = "10.32.40.242"
"dev-mqnameserver02" = "10.32.40.243"
"test-mqnameserver01" = "10.19.44.227"
"test-mqnameserver02" = "10.19.44.228"
}
$aname.keys | ForEach-Object{
$aaa = 'Add-DnsServerResourceRecordA -Name "{0}" -ZoneName "lianlianpay-inc-dc.com" -AllowUpdateAny -IPv4Address "{1}"' -f $_, $aname[$_]
#$aaa = '{0} = {1}' -f $_, $aname[$_]
Invoke-Expression $aaa
}
#OpenSSH连接后运行PowerShell命令
PowerShell -Command "&{Add-DnsServerResourceRecordA -Name "host23" -ZoneName "lianlianpay.com" -AllowUpdateAny -IPv4Address "172.18.99.23" -TimeToLive 01:00:00}"
##CMD下修改A记录 host23.lianlianpay.com 的IP指向为1.1.1.3 成功
Powershell -Command "& { $ZoneName = 'lianlianpay.com'; $HostName = 'host23'; $As = Get-DnsServerResourceRecord -ZoneName $ZoneName | ? {$_.HostName -eq $HostName} |sort RecordData; $NewRecord = $As.Clone(); $NewRecord.RecordData.IPv4Address ='1.1.1.3'; Set-DnsServerResourceRecord -ZoneName $ZoneName -OldInputObject $As -NewInputObject $NewRecord}"
#Linux下显示有问题,筛选只显示IP地址 CMD下不支持|管道符,通过以下操作可以支持管道符命令 成功
Powershell.exe -command "& {Get-DnsServerResourceRecord -ZoneName 'lianlianpay.com' -Name 'host23' -RRType 'A' | select -ExpandProperty RecordData}"
需求:
新增加域名:lianlianpay-inc-dc.com
批量新增加A记录
$aname = @{
"dev-mqnameserver01" = "10.32.40.242"
"dev-mqnameserver02" = "10.32.40.243"
"test-mqnameserver01" = "10.19.44.227"
"test-mqnameserver02" = "10.19.44.228"
"dev-zknode01" = "10.32.49.213"
"dev-zknode02" = "10.32.49.214"
"dev-zknode03" = "10.32.49.216"
"test-zknode01" = "10.19.44.236"
"test-zknode02" = "10.19.44.237"
"test-zknode03" = "10.19.44.238"
"test-zkxdts01" = "10.19.44.223"
"test-zkxdts02" = "10.19.44.224"
"test-zkxdts03" = "10.19.44.225"
"dev-kafka01" = "10.32.40.241"
"dev-kafka02" = "10.32.49.211"
"dev-kafka03" = "10.32.49.212"
"test-logkafka01" = "10.19.44.218"
"test-logkafka02" = "10.19.55.248"
"test-logkafka03" = "10.19.55.249"
"test-kafka01" = "10.19.44.221"
"test-kafka02" = "10.19.48.1"
"test-kafka03" = "10.19.48.2"
"redis-dev-sentinel1" = "10.32.40.239"
"redis-dev-sentinel2" = "10.32.49.207"
"redis-dev-sentinel3" = "10.32.49.208"
"redis-test-sentinel1" = "10.19.44.255"
"redis-test-sentinel2" = "10.19.45.0"
"redis-test-sentinel3" = "10.19.45.1"
"redis-test-cluster1" = "10.19.44.248"
"redis-test-cluster2" = "10.19.44.249"
"redis-test-cluster3" = "10.19.44.250"
"redis-test-cluster4" = "10.19.44.248"
"redis-test-cluster5" = "10.19.44.249"
"redis-test-cluster6" = "10.19.44.250"
"es7-test1" = "10.19.44.233"
"es7-test2" = "10.19.44.234"
"es7-test3" = "10.19.44.235"
"es6-test1" = "10.19.44.230"
"es6-test2" = "10.19.44.231"
"es6-test3" = "10.19.44.232"
"es-test-log1" = "10.19.44.251"
"es-test-log2" = "10.19.44.252"
"es-test-log3" = "10.19.44.253"
"es-test-log4" = "10.19.44.254"
"intra-dev" = "192.168.110.19"
"apollo-dev" = "10.32.49.215"
"apollo-ste" = "10.33.48.181"
"apollo-qa" = "10.19.48.5"
}
$aname.keys | ForEach-Object{
$aaa = 'Add-DnsServerResourceRecordA -Name "{0}" -ZoneName "lianlianpay-inc-dc.com" -AllowUpdateAny -IPv4Address "{1}"' -f $_, $aname[$_]
#$aaa = '{0} = {1}' -f $_, $aname[$_]
Invoke-Expression $aaa
}
参考:
https://learn.microsoft.com/zh-cn/previous-versions/windows/powershell-scripting/jj649850(v=wps.640)
https://woshub.com/create-manage-dns-zones-records-powershell/
https://blog.51cto.com/u_11508007/3593575