Hi James,
A couple of observations. As others have mentioned, the -Match conditional should return what you're looking for.
Also, you may want to avoid using the += as this increases processing time when the list of VMs gets large. Instead, look at the sample below.
Finally, I like to test using an internal table, which I then convert to a .CSV file.
Hope this helps.
# Sample .csv internal content using PSCustomObject
# This is useful during tests
#
$data = @(
[PSCustomObject]@{FQDN="dns.google"; IP="8.8.8.8"} # Should work
[PSCustomObject]@{FQDN="one.one.one.one"; IP="1.1.1.1"} # Will return general failure
[PSCustomObject]@{FQDN="dns.quad9.net"; IP="9.9.9.9"} # Should work
[PSCustomObject]@{FQDN="www.doofus.com"; IP="74.208.175.225"} # Should time out
)
# .csv Path
$CsvPath = ".\FQDN_Sorted.csv"
$CsvRept = ".\Report.csv"
# Export table to external .csv
$data | ConvertTo-Csv -NoTypeInformation | Set-Content $CsvPath
# Process .csv
$report = Import-Csv -Path $CsvPath |
ForEach-Object {
$result = ping -a -n 1 $_.IP
If ($result -Match "Reply") {
$result = "Success"
} else {
$result = "Fail"
}
[PSCustomObject] @{
Result = $result
VM = $_.FQDN
IP = $_.IP
}
}
$report | Export-Csv -Path $CsvRept -NoTypeInformation
Finally, instead of using ping, you may want to look at the Test-Connection PS statement. This returns True or False.
# Using PS test connection
$report = Import-Csv -Path $CsvPath |
ForEach-Object {
If (Test-Connection -ComputerName $_.IP -Count 1 -Quiet) {
$result = "Success"
} else {$result = "Fail"}
[PSCustomObject] @{
Result = $result
VM = $_.FQDN
IP = $_.IP
}
}
This makes the code a lot more compact and in my opinion, more elegant.
Cheers.
Original Message:
Sent: Feb 04, 2025 07:30 PM
From: James Dougherty
Subject: Find FQDNs for hostnames from a list of servers imported from csv
Here is a simple update based on what you have
The -n 1 for the ping command will speed up the ping results and overall report
For the FQDN, you just have to split the results and grab what you want for the FQDN.
$PingReport = @()
foreach ($VM in $VMs) {
$result = ping -a $vm.name -n 1
$FQDN = $Result.split()[2]
if ($result -Match "Reply") {
Write-Host "Pass"
$PingReport += [pscustomobject] @{
Ping = "Success";
FQDN = $FQDN;
}
}
Else {
Write-Host "Fail"
$PingReport += [pscustomobject] @{
Ping = "Fail";
FQDN = $VM.WhateverFromYourInput;
}
}
}
Original Message:
Sent: Feb 04, 2025 04:38 PM
From: dbutch1976
Subject: Find FQDNs for hostnames from a list of servers imported from csv
Hello,
I have a spreadsheet with a large number of hosts and IP addresses for which I need to find their FQDN's. Some of them may not even be valid IP addresses, some have no DNS A records associated with them.
I'm sure this has been done before, so if there's a better approach by all means let me know, but I was planning to ping each IP address, then I'll have to go back and update the hostname in the original spreadsheet.
I got started with something like this:
$VMs = Import-Csv -Path "C:\output\FQDN_Sorted.csv"$attempt = @()foreach ($VM in $VMs) { $result = ping -a $vm."IP Address" } if ($result -contains "Reply") { Write-Host "it triggered" $attempt += [PSCustomObject] @{ Name = $result } Write-Host "$result"}
However, even if $result contains reply example:
Pinging rq-in-f94.1e100.net [142.250.115.94] with 32 bytes of data:Reply from 142.250.115.94: bytes=32 time=59ms TTL=105Reply from 142.250.115.94: bytes=32 time=60ms TTL=105Reply from 142.250.115.94: bytes=32 time=86ms TTL=105Reply from 142.250.115.94: bytes=32 time=59ms TTL=105
It doesn't seem to trigger the if ($result -contains "Reply")
My questions are:
1. How can I select only the FQDN name from the successful ping replies?
2. Is there a better way to do this I'm not seeing?