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

后端 未结 4 1890
一生所求
一生所求 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:57

    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. These tweaks were needed to get it to output cleanly to the screen and to a txt file

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

提交回复
热议问题