Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Comparison operators and test-connection vs System.net.DNS

$
0
0

I have a script that gets a list of computers from AD (using get-qadcomputer), and then foreach object will use the test-connection cmdlet. It will then take the IPV4address property and run that through a cmdlet I've had for a while that uses [System.Net.DNS]::GetHostByAddress($address) to get the host name and compare that back. Basically this is checking to make sure that DNS matches AD, because we've had a few instances here where it sometimes does not.

It seems to be working just fine, except I'm not sure how to structure the comparison operator so that when a name is contained in a full DNS name it reports it as such. My script is as follows:

function Resolve-NameFromIP {
    param(
    [Parameter(Mandatory,ValueFromPipeline)]
    [string[]]
    $IPAddress
    )

    process {

        foreach ($addr in $IPAddress) {

            If (Test-Connection $addr -Count 1 -Quiet -ErrorAction SilentlyContinue ) {

                $details = [System.Net.DNS]::GetHostByAddress($addr)

                $props = @{
                    HostName = $details.HostName
                    IPAddress = $addr
                }
            }
            
            Else { $props = @{ HostName = 'IP DOES NOT RESOLVE' ; IPAddress = $addr } }

            New-Object PsObject -Property $props
        }
    }

}

#Prompt for AD Search Root
write-host "Enter name of Search Root to check (copy and paste cannonical name from AD Users and Computers)"
$searchroot = read-host

$count = 2
$xl = new-object -ComObject "Excel.Application"
$wb=$xl.workbooks.add()
$ws=$wb.activesheet
$ws.name = "Branch Server IP Check"
$xl.visible=$true
$cells=$ws.cells 
$cells.item(1,1) = "Server"
$cells.item(1,2) = "IP Address via ping"
$cells.item(1,3) = "DNS Entry via reverse"

get-qadcomputer -sizelimit 0 -searchroot $searchroot | ForEach-Object {

$servername = $_.name
$pingtest = test-connection -computername $servername
$resolveinfo = Resolve-NameFromIP $pingtest.IPV4Address
    
$cells.item($count,1) = $servername
$cells.item($count,2) = $resolveinfo.ipaddress
$cells.item($count,3) = $resolveinfo.hostname

if ($resolveinfo.hostname -contains $servername){
        $cells.item($count,4) = "Hostnames Identical (not FQDN)"
        $cells.item($count,4).Interior.ColorIndex = 3}
else {
        $cells.item($count,4) = "Hostnames FQDN"
        $cells.item($count,4).Interior.ColorIndex = 4}

$count = $count + 1
    }

So that first function does the resolving a name from an IP address, and then it prompts the user for the OU, creates the Excel sheet and populates it. It seems to run fineexcept for that comparison operator. What I want it to do is tell me 1 of three things:

1) If the servername from AD is is contained in the full DNS name

(I.E. server1 is reported back by test-connection and Resolve-NameFromIP returns server1.domain.local)

2) If the "full" DNS name reporting back is not a full DNS name

(I.E. server2 is reported back by test-connection and Resolve-NameFromIP returns just server2 - no domain.local)

3) If for some strange reason the servername from AD is neither contained nor equal to the "full" DNS name

(I.E. test-connection reports back server3 and for some reason Resolve-NameFromIP returns workstation1 or server9)

I'm guessing this is a problem of test-connection reporting back properties which might not be strings?


zarberg@gmail.com



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>