【问题标题】:Ping a list of host names and output the results to a csv in powershellPing 主机名列表并将结果输出到 powershell 中的 csv
【发布时间】:2015-06-12 03:46:59
【问题描述】:

我有一大串主机名,我需要 ping 以查看它们是启动还是关闭。我不是很擅长编写脚本,但我设法弄清楚了这一点:

$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
  }
}

这得到了我需要的东西,但我现在需要将这些结果写到一个 csv 文件中,我不知道该怎么做。

请帮忙!

【问题讨论】:

    标签: loops powershell csv ping


    【解决方案1】:

    您可以改用以下代码(我只是将 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"
      }
    }
    

    附:您也可以使用Out-File Cmdlet 创建 CSV 文件

    【讨论】:

    • 甜蜜!这行得通!有没有办法让脚本本身打印到 csv 文件而不是管道?我需要把这个交给我的一个同事,他宁愿只运行脚本。
    • 是的,您可以使用 Add-Content cmdlet 代替 Write-host cmdlet,例如 Add-Content output.csv "$name,up"
    • 现在有了写CSVExport-Csv和ConvertTo-Csv的函数——记录ping我发现json更容易使用,比如:Test-Connection -TargetName $targets -Ping -OutVariable ping_results; $ping_results | ConvertTo-Json -AsArray -Compress -OutVariable last_export | Out-File -FilePath "log.json" -Append;
    【解决方案2】:

    我是 Powershell 的新手,因此我将其作为一项学习任务,因为我需要一种快速简单的方法来检查 PC 列表的启动/关闭状态。需要进行这些调整才能使其干净地输出到屏幕和 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"
    

    【讨论】:

    • 漂亮干净。谢谢
    【解决方案3】:

    我会这样做。使用计算机列表和 -asjob 效果很好。如果主机启动,则 Responsetime 属性(令人困惑的标题是“Time(ms)”)将为非空。

    $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
    

    【讨论】:

    • ...你在 js2010 中忘记了什么吗?输出在哪里?
    【解决方案4】:
        $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"
    

    这有点干净,包括原始的前景选项,但顺便说一句,“延迟”开关似乎被忽略了 -PB

    【讨论】:

    • 顺便说一句,Powershell 6 的测试连接超时选项“延迟”选项错误。 “延迟”什么都不做,之前的定时选项不再存在……所以你将以最大速度 ping……PB
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2012-11-29
    • 1970-01-01
    • 2017-04-20
    相关资源
    最近更新 更多