简体   繁体   English

PowerShell 测试连接返回设备已关闭,但未关闭

[英]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.我在 PowerShell 中使用以下代码来 ping 多个设备并返回它们是启动还是关闭。

$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.我已经使用过 Count 4 和 Delay 2,但我不确定其他参数是否能解决问题。

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.每隔一段时间,代码会返回一个设备已关闭,但是当我 go 检查设备时,我有一个连接。

This may not be your problem, but you didn't specify if the devices have a static IP or dynamic IP.这可能不是您的问题,但您没有指定设备是否具有 static IP 或动态 IP。 If dynamic, I would suggest always running ipconfig /flushdns prior to running your script.如果是动态的,我建议在运行脚本之前始终运行ipconfig /flushdns 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.我无法告诉您有多少次我认为某个设备已关闭,或者认为我正在连接到一个设备却发现我正在连接到另一个设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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