3

When the server is ping'd the resulting address is compared to an inventory for purposes of keeping the inventory up to date.

I'm trying to tack on the result as "good" or "bad" in the next corresponding cell over. It sort of works except the result is always "bad" with the workflow I have set up.

The CSV contains the server name and IP address pulled from an excel inventory, and is formatted like this:

name,ipaddress
server1,10.1.24.51
server2,10.1.24.52
server3,10.1.24.53
server4,10.1.27.101
server5,10.1.27.102 <--- purposely wrong IP address for testing

Current script:

$serverlist = Import-Csv -Path file.csv

ForEach ($server in $serverlist) {

    $thisservername = $server.name
    $thisserveripaddress = $server.ipaddress

    $pingdaddress = (Test-Connection -ComputerName $thisservername -Count 1 -ErrorAction SilentlyContinue -Verbose).IPV4Address.IPAddressToString

    if ($pingdaddress -ne $thisserveripaddress) {

        #$thisservername + " bad"
        $serverlist | Select-Object name,ipaddress, @{Name='connection';Expression={'bad'}} | `
        Export-Csv -Path file.csv -NoTypeInformation

    } else {

        #$thisservername + " good"
        $serverlist | Select-Object name,ipaddress, @{Name='connection';Expression={'good'}} | `
        Export-Csv -Path file.csv -NoTypeInformation

    }

}

1 Answer 1

5

I think your error is stemming from $pingdaddress = (Test-Connection -ComputerName $thisservername -Count 1 -ErrorAction SilentlyContinue -Verbose).IPV4Address.IPAddressToString. If the server name cannot be resolved, the connection object (well, Win32_PingStatus object) returned by Test-Connection will be $null. You then are trying to access a property on a null object, which isn't allowed.

I would split the 'success' portion into it's own column. You can do this by adding another column to your CSV, e.g.: NameMatch or IPMatch, whatever makes more sense to you. This way you can access it as a property in your loop $server.NameMatch and do filtering/sorting on the data later.

function Test-ServerNameMapping
{

    [cmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateScript({Test-Path $_})]
        [String]
        $Path
    )

    $serverList = Import-Csv -Path $Path

    foreach ($server in $serverList)
    {
        Write-Verbose "Testing: $($server.Name), $($server.IPAddress)"

        $connectionObject = (Test-Connection -ComputerName $server.Name -Count 1 -ErrorAction SilentlyContinue)

        if (-not $connectionObject)
        {
            Write-Verbose "Failed to resolve $($server.Name) to an IP address."
            $server.namematch = $false
        }
        else
        {
            $resolvedAddress = $connectionObject.IPV4Address.ToString()
            Write-Verbose "Resolved $($server.Name) to $resolvedAddress"

            if ($resolvedAddress -eq $server.IPAddress)
            {
                Write-Verbose "$($server.IPAddress) matches found address of $resolvedAddress"
                $server.namematch = $true
            }
            else
            {
                Write-Verbose "$($server.IPAddress) does not equal found address of $resolvedAddress"
                $server.namematch = $false
            }
        }
    }

    $serverList | Export-Csv -Path $Path -NoTypeInformation -Force

}

Then if you wanted to produce a report later you could do something like:

$problemServers = Import-Csv -Path c:\example.csv | ? NameMatch -eq $false

Example.csv:

Name,IPAddress,NameMatch
server1,10.0.0.1,"True"
server2,10.0.0.2,
server3,10.0.0.3,"False"

When you first run the script, the NameMatch column can be empty (as it is with server2), and the script will fill it in.

2
  • 1
    This worked freakishly well. Thank you Phil! I've been fighting this for days. I don't see any verbose output on my end though, which I find very helpful, it just runs. Jun 27, 2017 at 21:51
  • NP. It's a function, so just add the -Verbose switch at the end. E.x.: Test-ServerNameMapping -Path c:\example.csv -Verbose
    – Epic Phil
    Jun 28, 2017 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .