locked
Test-connection Multiple servers RRS feed

  • Question

  • I have 100 + servers to test , is giving incorrect results. but when replace $Computers = 'a','b','c','d'... the script is working.

    no idea how to resolve this issue , please guide

    $Computers=get-content "C:\Users\abc\Desktop\PowerShell\T1\test_server.txt"


    Foreach ($Computer in $a)
    {

       write-host $Computer
       if(!(Test-Connection -Cn $Computer -Count 1 -ea 0 -quiet))
       { "Bad" } else {"good"}

    }

           
    Friday, July 14, 2017 8:42 PM

All replies

  • Hi Sreenivasa G 

    Not sure what inccorect result you are seeing but below is a script I use to test if a list of computer are up or down. I just add the computer names to a txt. 

    $computers = Get-Content "C:\Export\Ping.txt"
    foreach ($computer in $computers)
    {
        $Destination = "C:\Export\Pingup.log"
        $Destination2 = "C:\Export\PingDown.log"
        if (Test-Connection $computer -Count 1 -ea 0 -Quiet)
        { 
            Write-Host "$computer Is Up" -ForegroundColor Green
            $computer | out-file -Append $Destination -ErrorAction SilentlyContinue 
        } 
        else 
        { 
            Write-Host "$computer Is Down" -ForegroundColor Red
            $computer | out-file -Append $Destination2 -ErrorAction SilentlyContinue  
        }
      
    }

    Friday, July 14, 2017 9:13 PM
  • Your file must be one computer name per line and no blank lines.

    Get-Content C:\Users\abc\Desktop\PowerShell\T1\test_server.txt | ForEach-Object{ if(Test-Connection $_ -Count 1 -quiet){ "$_ Good" }else{ "$_ Bad" }
    }

    Don't use EA 0 as it will mask file content errors. Test does not throw errors if the computername is never null.


    \_(ツ)_/



    • Edited by jrv Friday, July 14, 2017 9:39 PM
    Friday, July 14, 2017 9:36 PM
  • Shouldn't that be:

    $a = get-content "C:\Users\abc\Desktop\PowerShell\T1\test_server.txt"

    Works for me.  What does test_server.txt look like?

    Here's mine:

    Function get-ping  {
    
      Param (
        [parameter(ValueFromPipeline)]
        [string[]]$hostname='yahoo.com'
      )
    
      Begin {
        $ping = New-Object System.Net.Networkinformation.ping
        $timeout = 500 # ms
      }
    
      Process {
        $hostname | foreach {
          $ping.Send($_, $timeout) | add-member -passthru hostname $_ |
          select hostname,address,status,roundtriptime
        } 
      }
    }

    You can do $a | get-ping or get-ping $a ...

    EDIT:

    cool thing 

    1..100 | foreach { '{0:d3}' -f $_ -replace '^','computer' }  | get-ping  





    • Proposed as answer by jrv Friday, July 14, 2017 10:38 PM
    • Edited by JS2010 Monday, July 17, 2017 2:42 PM
    Friday, July 14, 2017 10:02 PM
  • Shouldn't that be:

    $a = get-content "C:\Users\abc\Desktop\PowerShell\T1\test_server.txt"

    Works for me.

    What are you referring to?


    \_(ツ)_/

    Friday, July 14, 2017 10:04 PM
  • The original post.

    Friday, July 14, 2017 10:34 PM
  • The original post.

    Yes, that would help.


    \_(ツ)_/

    Friday, July 14, 2017 10:37 PM