Powershell DNS - 如何搜尋 DNS 伺服器清單然後過濾結果?

Powershell DNS - 如何搜尋 DNS 伺服器清單然後過濾結果?

我正在嘗試搜尋特定 DNS 伺服器的清單(我在檔案中),然後查詢特定的主機名稱。我可以做到這一點:)

下一點是我希望返回那些返回 Outlook-emea* 之外的結果的 DNS 伺服器的列表,我想要 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 時的結果:

DNS查詢結果

相關內容