[英]Check DNS to see if Record Exists
我正在尝试创建一个 PowerShell 脚本来执行以下操作:
我的问题是,当它检查 DNS 中是否存在记录时,它说该记录不存在,然后尝试创建它,而实际上它确实存在于 DNS 中。 这是我的脚本:
# Declare Variables. These variables are different in each domain
$ZoneName = "domain.com"
$DNSServer = "domain1.domain.com"
$ExistsInDNS = "C:\myScripts\DNSScavenging\RestoreDNSRecords\ExistsInDNS.txt"
$RestoredToDNS = "C:\myScripts\DNSScavenging\RestoreDNSRecords\RestoredToDNS.txt"
$DNSRecords = Import-Csv -path "C:\myScripts\DNSScavenging\RestoreDNSRecords\dnsrestoretest.csv" -Header "Server","IP"
foreach ($DNSRecord in $DNSRecords){
# Check DNS to see if the Server and IP exists
write-host "Checking to see if $($DNSRecord.Server) exists in DNS"
$DNSCheck = [System.Net.DNS]::GetHostByName($DNSRecord.Server)
if ($($DNSCheck.HostName) -contains $($DNSRecord.Server)) {
write-host "$($DNSRecord.Server) $($DNSRecord.IP) exists in DNS" -ForegroundColor "Green"
write-output "$($DNSRecord.Server) $($DNSRecord.IP)" | out-file $ExistsInDNS -Append
} else {
write-host "$($DNSRecord.Server) $($DNSRecord.IP) does not exist in DNS. Restoring $($DNSRecord.Server) $($DNSRecord.IP) in DNS" -ForegroundColor "Yellow"
write-output "$($DNSRecord.Server) $($DNSRecord.IP)" | out-file $RestoredToDNS -Append
# Add record into DNS
Add-DnsServerResourceRecordA -ComputerName $DNSServer -ZoneName "$ZoneName" -Name $DNSRecord.Server -AllowUpdateAny -IPv4Address $DNSRecord.IP
}
}
我建议你使用:
Resolve-DnsName -Name "DNSEntry" -Server "yourserver"
这样你就可以直接检查你的目标 dns 服务器,你只需要检查它是否返回 true。
对于偶然发现此问题的任何其他人,我会为您节省几个小时的时间。 像下面这样格式化 .csv,包括标题,但在这种情况下不要使用 fqdn,只使用主机名。
name,ip
testnameexample,192.168.1.1
并使用此代码:
#Declare Variables. These variables are different in each domain
$ZoneName = "yourdomain.com"
#$DNSServer = "yourdnsserver.yourdomain.com"
$ExistsInDNS = "C:\temp\ExistsInDNS.txt"
$RestoredToDNS = "C:\temp\AddedToDNS.txt"
$DNSRecords = Import-Csv -path "C:\temp\IP_LIST.csv" -Header "name","ip"
foreach ($DNSRecord in $DNSRecords){
# Check DNS to see if the Server and IP exists
write-host "Checking to see if $($DNSRecord.name) exists in DNS"
$DNSCheck = $(resolve-DnsName -name "$($DNSRecord.name).$ZoneName" -erroraction 'silentlycontinue' | select-object Name)
write-host "DNS Lookup Result [blank if not found]: $($DNSCheck.Name)"
if ($($DNSCheck.Name) -match $($DNSRecord.name)) {
write-host "$($DNSRecord.name) $($DNSRecord.ip) exists in DNS, Skipping.." -ForegroundColor "Green"
write-output "$($DNSRecord.name) $($DNSRecord.ip)" | out-file $ExistsInDNS -Append
} else {
write-host "$($DNSRecord.name) $($DNSRecord.ip) does not exist in DNS. Adding $($DNSRecord.name) $($DNSRecord.ip) in DNS" -ForegroundColor "Yellow"
write-output "$($DNSRecord.name) $($DNSRecord.ip)" | out-file $RestoredToDNS -Append
# Add record into DNS
#Add-DnsServerResourceRecordA -ComputerName $DNSServer -ZoneName "$ZoneName" -Name $($DNSRecord.name) -AllowUpdateAny -IPv4Address $($DNSRecord.ip)
Add-DnsServerResourceRecordA -ZoneName "$ZoneName" -Name $($DNSRecord.name) -AllowUpdateAny -IPv4Address $($DNSRecord.ip)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.