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 쿼리 결과

관련 정보