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
}
}
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 byTest-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
orIPMatch
, 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.Then if you wanted to produce a report later you could do something like:
Example.csv:
When you first run the script, the NameMatch column can be empty (as it is with server2), and the script will fill it in.