# 设置 DNS 服务器
$DNSServer = "localhost" # 替换为实际的 DNS 服务器名称
# 获取所有 DNS 区域
$zones = Get-WmiObject -Namespace "root\MicrosoftDNS" -Query "SELECT * FROM MicrosoftDNS_Zone" -ComputerName $DNSServer
# 初始化一个空的数组来存储 DNS 记录
$dnsRecords = @()
foreach ($zone in $zones) {
$zoneName = $zone.Name
Write-Output "处理区域: $zoneName"
# 获取每个区域的所有 A 记录
$aQuery = "SELECT * FROM MicrosoftDNS_AType WHERE ContainerName = '$zoneName'"
try {
$aRecords = Get-WmiObject -Namespace "root\MicrosoftDNS" -Query $aQuery -ComputerName $DNSServer
} catch {
Write-Error "无法获取 A 记录: $_"
continue
}
foreach ($record in $aRecords) {
$dnsRecord = [PSCustomObject]@{
ZoneName = $zoneName
DomainName = $record.OwnerName
RecordType = "A"
IPAddress = $record.RecordData
TargetName = $null # CNAME记录没有 IP 地址,所以用 $null
}
$dnsRecords += $dnsRecord
}
# 获取每个区域的所有 CNAME 记录
$cnameQuery = "SELECT * FROM MicrosoftDNS_CNAMEType WHERE ContainerName = '$zoneName'"
try {
$cnameRecords = Get-WmiObject -Namespace "root\MicrosoftDNS" -Query $cnameQuery -ComputerName $DNSServer
} catch {
Write-Error "无法获取 CNAME 记录: $_"
continue
}
foreach ($record in $cnameRecords) {
$dnsRecord = [PSCustomObject]@{
ZoneName = $zoneName
DomainName = $record.OwnerName
RecordType = "CNAME"
IPAddress = $null # CNAME记录没有 IP 地址,所以用 $null
TargetName = $record.PrimaryName
}
$dnsRecords += $dnsRecord
}
}
# 导出到 CSV 文件
if ($dnsRecords.Count -gt 0) {
$dnsRecords | Export-Csv -Path "C:\dns_records.csv" -NoTypeInformation
Write-Output "DNS 记录已导出到 C:\dns_records.csv"
} else {
Write-Output "没有 DNS 记录可导出"
}
如何导出Windows 服务器上DNS解析记录值
最新推荐文章于 2024-08-27 22:51:12 发布