#查询DNS可用类
Get-WmiObject -Namespace root\MicrosoftDNS -List
#查询所有资源记录
$mydns = [WMIClass]"ROOT\MicrosoftDNS:MicrosoftDNS_ResourceRecord" $mydns = [WMIClass]"\\$Server\ROOT\MicrosoftDNS:MicrosoftDNS_ResourceRecord"
#查询已有区域
gwmi -Namespace root\microsoftdns -Class MicrosoftDNS_zone |select ContainerName
#查询 ddv.com区域下的所有A记录
Get-WmiObject -Namespace root\MicrosoftDNS -class microsoftdns_atype | ? {$_.ContainerName -eq "ddv.com"} |Select TextRepresentation
#查询单条A记录
#查询 bb.ddv.com 的A记录 $ARecord = Get-WmiObject -Namespace root\MicrosoftDNS -class MicrosoftDNS_ResourceRecord -filter "containername='ddv.com' AND OwnerName='bb.ddv.com'"
#查询 bb.ddv.com 的A记录 $ARecord = Get-WmiObject -Namespace root\MicrosoftDNS -class microsoftdns_atype | ? {$_.OwnerName -eq "bb.ddv.com"}
#修改A记录
#修改A记录 bb.ddv.com 的IP指向为172.0.0.23
$ARecord = Get-WmiObject -Namespace root\MicrosoftDNS -class microsoftdns_atype | ? {$_.OwnerName -eq "bb.ddv.com"}
$ARecord.Modify($ARecord.TTL,"127.0.0.223")
#删除A记录
#删除A记录 bb.ddv.com $ARecord = Get-WmiObject -Namespace root\MicrosoftDNS -class microsoftdns_atype | ? {$_.OwnerName -eq "bb.ddv.com"} $ARecord.delete()
#添加A记录
#添加单条A记录 bb.ddv.com IN A 127.0.0.3 $dnsServerName="" $containerName="ddv.com" $dns = [WMIClass]"ROOT\MicrosoftDNS:MicrosoftDNS_ResourceRecord" $dns.CreateInstanceFromTextRepresentation($dnsServerName, $containerName, "bb.ddv.com IN A 127.0.0.3")
#批量添加A记录 $dnsServerName="" $containerName="ddv.com" $dns = [WMIClass]"ROOT\MicrosoftDNS:MicrosoftDNS_ResourceRecord" Get-Content D:\dnsListFile.txt | Foreach-Object {$dns.CreateInstanceFromTextRepresentation($dnsServerName, $containerName, $_)}
dnsListFile.txt内容如下:
aaa.ddv.com IN A 127.0.0.1
bb.ddv.com IN A 127.0.0.2
#查询转发服务器
gwmi -Namespace "root\MicrosoftDNS" -class "MicrosoftDNS_Server" | Select-Object -ExpandProperty Forwarders
=====================================================================
#修改DNS A记录 $ZoneName = "yxp.com" $HostName = "ldaptest" $As = Get-DnsServerResourceRecord -ZoneName $ZoneName | ? {$_.HostName -eq $HostName} |sort RecordData $NewRecord = $As.Clone() $NewRecord.RecordData.IPv4Address ="10.10.3.3" Set-DnsServerResourceRecord -ZoneName $ZoneName -OldInputObject $As -NewInputObject $NewRecord #添加A记录 Add-DnsServerResourceRecordA -Name $HostName -ZoneName $ZoneName -AllowUpdateAny -IPv4Address $IP
#修改A记录TTL值 $OldObj=Get-DnsServerResourceRecord -ZoneName "xx.com" -RRType "A" foreach ($record in $OldObj) { $NewObj = $record.Clone() $NewObj.TimeToLive = [System.TimeSpan]::FromHours(6) Set-DnsServerResourceRecord -NewInputObject $NewObj -OldInputObject $record -ZoneName "xx.com" -PassThru $NewObj=$Null $record=$Null }
From:http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/13/manage-dns-in-a-windows-environment-by-using-powershell.aspx