简体   繁体   English

Ping 主机名列表并将结果输出到 powershell 中的 csv

[英]Ping a list of host names and output the results to a csv in powershell

I have a large list of hostnames I need to ping to see if they are up or down.我有一个很大的主机名列表,我需要 ping 以查看它们是启动还是关闭。 I'm not really that great at scripting but I managed to figure this much out:我在编写脚本方面并不是很擅长,但我设法弄清楚了这一点:

$names = Get-content "hnames.txt"

foreach ($name in $names){
  if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
    Write-Host "$name is up" -ForegroundColor Green
  }
  else{
    Write-Host "$name is down" -ForegroundColor Red
  }
}

This gets me what I need but i now need to write out these results to a csv file and i have no idea how to do that.这让我得到了我需要的东西,但我现在需要将这些结果写到一个 csv 文件中,我不知道该怎么做。

Please Help!请帮忙!

You can use the following code instead (I simply altered the write-host calls to CSV formatting) and execute it with "PowerShell.exe script.ps > output.csv" Note that you must execute it from the folder that contains hnames.txt, or simply change the "hnames.txt" to a full path.您可以改用以下代码(我只是将 write-host 调用更改为 CSV 格式)并使用“PowerShell.exe script.ps > output.csv”执行它请注意,您必须从包含 hnames.txt 的文件夹中执行它,或者简单地将“hnames.txt”更改为完整路径。

$names = Get-content "hnames.txt"

foreach ($name in $names){
  if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
    Write-Host "$name,up"
  }
  else{
    Write-Host "$name,down"
  }
}

PS You can also use the Out-File Cmdlet to create the CSV file PS 您还可以使用Out-File Cmdlet创建 CSV 文件

I am a complete newbie to Powershell, so I took this on as a learning task, as I needed a quick and simple way to check a list of PC's for up/down status.我完全是 Powershell 的新手,所以我把它作为一项学习任务,因为我需要一种快速而简单的方法来检查 PC 列表的启动/关闭状态。 These tweaks were needed to get it to output cleanly to the screen and to a txt file需要进行这些调整才能将其干净地输出到屏幕和 txt 文件

$Output= @()
$names = Get-content "hnames.txt"
foreach ($name in $names){
  if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
   $Output+= "$name,up"
   Write-Host "$Name,up"
  }
  else{
    $Output+= "$name,down"
    Write-Host "$Name,down"
  }
}
$Output | Out-file "C:\support\result.csv"
    $Output= @()
    $names = Get-Content ".\input\Servers.txt"
    foreach ($name in $names){
      if (Test-Connection -Delay 15 -ComputerName $name -Count 1 -ErrorAction SilentlyContinue -quiet){
       $Output+= "$name,up"
       Write-Host "$Name,up" -ForegroundColor Green
      }
      else{
        $Output+= "$name,down"
        Write-Host "$Name,down" -ForegroundColor Red
      }
    }
    $Output | Out-file ".\output\result.csv"

This is a tad cleaner, and includes the original foreground options but, BTW, the 'delay' switch seems to be ignored -PB这有点干净,包括原始的前景选项,但顺便说一句,“延迟”开关似乎被忽略了 -PB

I would do it this way.我会这样做。 Using a list of computers and -asjob works very well.使用计算机列表和 -asjob 效果很好。 The Responsetime property (confusingly the header is "Time(ms)") will be non-null if the host is up.如果主机启动,响应时间属性(令人困惑的标题是“时间(毫秒)”)将为非空。

$names = Get-content hnames.txt
test-connection $names -asjob -count 1 | receive-job -wait -auto
Source        Destination     IPV4Address      IPV6Address     Bytes    Time(ms)
------        -----------     -----------      -----------     -----    --------
COMP001       yahoo.com       74.6.231.21                      32       39
COMP001       microsoft.com   40.113.200.201                   32

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

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