I have this script to check Ping, RDP, and NSLookup for a bulk server but when I get results for the ping portion, instead of true I am getting time out "False" results".
$list = gc -Path C:\Scripts\hostlist.txt
foreach ($server in $list){
$result = Test-NetConnection $server -Port 3389
$output = [PSCustomObject]@{
"Hostname" = $server
"Ping_Pass_?" = $result.PingSucceeded
"RDP_Pass_?" = $result.TcpTestSucceeded
"DNS_Resolved_?" = $result.NameResolutionSucceeded
}
$output | select -Property Hostname,Ping_Pass_?,RDP_Pass_?,DNS_Resolved_?
}
$list = gc -Path C:\temp\server.txt
$output = ""
foreach ($server in $list){
$result = Test-NetConnection -ComputerName $server -Port 3389
$output = [PSCustomObject]@{
"Hostname" = $server
"Ping_Pass_?" = $result.PingSucceeded
"RDP_Pass_?" = $result.TcpTestSucceeded
"DNS_Resolved_?" = $result.NameResolutionSucceeded
}
$output | select -Property Hostname,Ping_Pass_?,RDP_Pass_?,DNS_Resolved_? | Export-Csv C:\scripts\ping_rdp_nslookup_result.csv -Append
}
CodePudding user response:
That's simple, it is because you aren't attempting a ping. You have specified a port to which it should attempt the connectivity test (3389, the standard RDP port). As such it is not attempting to ping the destination server. If you want to do that you can perform a separate call on that line:
"Ping_Pass_?" = Test-NetConnection $server -InformationLevel Quiet
Your script, with those two lines replaced, would look like this:
$list = gc -Path C:\Scripts\hostlist.txt
foreach ($server in $list){
$result = Test-NetConnection $server -Port 3389
$output = [PSCustomObject]@{
"Hostname" = $server
"Ping_Pass_?" = Test-NetConnection $server -InformationLevel Quiet
"RDP_Pass_?" = $result.TcpTestSucceeded
"DNS_Resolved_?" = $result.NameResolutionSucceeded
}
$output | select -Property Hostname,Ping_Pass_?,RDP_Pass_?,DNS_Resolved_?
}
$list = gc -Path C:\temp\server.txt
$output = ""
foreach ($server in $list){
$result = Test-NetConnection -ComputerName $server -Port 3389
$output = [PSCustomObject]@{
"Hostname" = $server
"Ping_Pass_?" = Test-NetConnection $server -InformationLevel Quiet
"RDP_Pass_?" = $result.TcpTestSucceeded
"DNS_Resolved_?" = $result.NameResolutionSucceeded
}
$output | select -Property Hostname,Ping_Pass_?,RDP_Pass_?,DNS_Resolved_? | Export-Csv C:\scripts\ping_rdp_nslookup_result.csv -Append
}