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

后端 未结 4 1889
一生所求
一生所求 2020-12-16 07:52

I have a large list of hostnames I need to ping to see if they are up or down. I\'m not really that great at scripting but I managed to figure this much out:



        
4条回答
  •  星月不相逢
    2020-12-16 08:44

        $Output= @()
        $names = Get-Content ".\input\Servers.txt"
        foreach ($name in $names){
          if (Test-Connection -Delay 15 -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
           $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

提交回复
热议问题