我正在尝试搜索特定 DNS 服务器列表(我在一个文件中),然后查询特定主机名。我可以做这一点 :)
下一步是我希望返回那些返回结果的 DNS 服务器列表,除了 outlook-emea* 之外,我还想要 DNS 服务器的 IP 以及结果。
我遇到的问题是 DNS 命令返回 CNAMES 和 A 记录 - 我只对 A 记录感兴趣,而且我不确定如何过滤结果。这是我目前所拥有的。
$Address = 'outlook.office365.com'
#$listofIPs = Get-Content 'C:\Users\user1\file.txt'
$listofIPs = '8.8.8.8'
$ResultList = @()
foreach ($ip in $listofIPs)
{
$Result = Resolve-DnsName -Name $Address -Type A -Server $ip
Write-Host ""
Write-Host DNS Server: -foregroundcolor "green" $ip
Write-Host ""
Write-Host Resolved Names: -foregroundcolor "green"
}
有人可以帮忙吗?
答案1
这是我目前根据您的脚本编写的脚本:
$Address = "outlook.office365.com"
$listofIPs = Get-Content "C:\file.txt"
$ResultList = @()
foreach ($ip in $listofIPs)
{
# The following query will list only records begining with "outlook-", but not begining with "outlook-emea"
$DNSquery = (Resolve-DnsName -Name $Address -Type A -Server $ip).Name | Where-Object {$_ -inotlike "outlook-emea*" -and $_ -ilike "outlook-*"}
# We assume, based on several tests, that selecting the first result for the previous query is enough.
$Result = $DNSquery | Select -First 1
if ($DNSquery)
{
# Creating custom object to feed the array
$Object = New-Object PSObject
$Object | Add-Member -MemberType NoteProperty -Name "DNS Server IP" -Value $ip
$Object | Add-Member -MemberType NoteProperty -Name "Result" -Value $Result
$ResultList += $Object
}
# Displaying the array with the results
$ResultList
}
当我的文本文件包含 8.8.8.8、8.8.8.4、173.255.0.194 和 173.201.20.134 时,得到的结果如下: