简体   繁体   中英

PowerShell Test-Connection is returning that a device is down when it is not

I am using the following code in PowerShell to ping multiple devices and return whether they are up or down.

$Output= @()
$names = Get-content "C:\HHM\hnames.txt"
foreach ($name in $names){
  if (Test-Connection -ComputerName $name -Count 4 -Delay 2 -ErrorAction SilentlyContinue){
   $Output+= "$name,up"
   Write-Host "$Name,up"
  }
  else{
    $Output+= "$name,down"
    Write-Host "$Name,down"
  }
}
$Output | Out-file "C:\HHM\result.csv"

When I check some of the devices that are listed as down, they are actually up. Given the nature of these devices, I do not believe that the connection is intermittent and the connection was actually down when the code was run. Would any of the other Test-Connection Parameters potentially help this? I've already used Count 4 and Delay 2, but I'm not sure if any of the other parameters will solve the issue.

EDIT - Just to clarify, I am getting the correct up/down status most of the time. Every once and a while, the code will return that a device is down, but when I go to check the device, I have a connection.

This may not be your problem, but you didn't specify if the devices have a static IP or dynamic IP. If dynamic, I would suggest always running ipconfig /flushdns prior to running your script. I can not begin to tell you how many times I thought a device was down, or thought I was connecting to one device only to discover I was connecting to another device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM