检查DNS以查看记录是否存在

时间:2018-07-16 15:07:20

标签: windows powershell server dns

我正在尝试创建PowerShell脚本来执行以下操作:

  1. 打开我的CSV文件。列A具有服务器的主机名。 B列具有IP地址
  2. 然后我想查看DNS中是否存在主机名。我检查第一个主机名,如果该主机存在,请继续执行下一个主机名。如果没有,请使用主机名和IP将新的A记录添加到DNS中。

我的问题是,当它进行检查以查看记录是否在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
    }
}

2 个答案:

答案 0 :(得分:1)

我建议您使用:

Resolve-DnsName -Name "DNSEntry" -Server "yourserver"

这样,您可以直接检查目标dns服务器,只需要检查它是否返回true。

答案 1 :(得分:0)

对于偶然发现此问题的任何其他人,我会为您节省几个小时的时间。像下面这样格式化 .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)
    }
}