none
Script to ping multiple servers RRS feed

  • Question

  • I am looking for a powershell script to ping multiple computers in a text file, and return the computer name and true or false for ping status.
    Wednesday, September 12, 2018 1:30 PM

Answers

  • Please format your code as code here in the forum. Thanks.
    $ServerName = Get-Content(servers.txt)
    $Result = foreach ($Server in $ServerName) { 
        [PSCustomObject]@{
            Server = $Server
            Ping = Test-Connection -ComputerName $Server -Count 1 -Quiet
        }
    } 
    $Result 
    $Result | Export-Csv -Path C:\_Temp\ServerPing.CSV -NoTypeInformation
    You should either learn how to google for simple Powershell scripts or at least the basics of Powershell. That's a beginner question.  ;-) :-D

    Best regards,

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, September 13, 2018 12:50 PM
    • Proposed as answer by jrv Thursday, September 13, 2018 4:00 PM
    • Marked as answer by Richard MuellerMVP, Moderator Thursday, September 20, 2018 1:24 PM
    Thursday, September 13, 2018 12:47 PM

All replies

  • Then start looking, Google and our Gallery are your friends.

    This forum is for scripting help for people that know how to use PowerShell and are having specific issues with a script. What have you tried so far and what is going wrong?

    Wednesday, September 12, 2018 1:38 PM
  • Hi!

    I suggest you look in the TechNet Gallery, there are many scripts available there.

    Here's one for script for example:

    Powershell Script to ping and report on computers, export to CSV

    Best regards,
    Leon


    Blog: https://thesystemcenterblog.com LinkedIn:

    Wednesday, September 12, 2018 1:40 PM
  • I have this:

    $ServerName = Get-Content(servers.txt)
    foreach ($Server in $ServerName) { 
     
            if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {  
             
                write-Host "$Server is alive and Pinging " 
             
                        } else 
                         
                        { Write-Warning "$Server seems dead not pinging" 
                 
                        }     
     However I want the output in a file with the servername in one column and ping status in another column.

     

    Thursday, September 13, 2018 12:17 PM
  • Please format your code as code here in the forum. Thanks.
    $ServerName = Get-Content(servers.txt)
    $Result = foreach ($Server in $ServerName) { 
        [PSCustomObject]@{
            Server = $Server
            Ping = Test-Connection -ComputerName $Server -Count 1 -Quiet
        }
    } 
    $Result 
    $Result | Export-Csv -Path C:\_Temp\ServerPing.CSV -NoTypeInformation
    You should either learn how to google for simple Powershell scripts or at least the basics of Powershell. That's a beginner question.  ;-) :-D

    Best regards,

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, September 13, 2018 12:50 PM
    • Proposed as answer by jrv Thursday, September 13, 2018 4:00 PM
    • Marked as answer by Richard MuellerMVP, Moderator Thursday, September 20, 2018 1:24 PM
    Thursday, September 13, 2018 12:47 PM
  • try this

    <#.\Server.txt
    DC01
    DC02
    #>
    
    $result=@()
    Get-Content .\Servers.txt | %{
    $start_name = $_
    
    $conn = Test-Connection -ComputerName $_ -Quiet
    if(-not $conn)
    {
      $start_name = ""
    }
    
    Try
    { 
      $dns = [System.Net.Dns]::GetHostEntry($_)
      $dns_host = $dns.HostName
      $dns_ip  = $dns.AddressList | select -ExpandProperty IPAddressToString
    }
    catch
    {
      $dns_host = "invalid host name"
      $dns_ip = "invalid host name"
      $start_name = ""
    }
    
    $HostObj = New-Object PSObject -Property @{
    									Host    = $start_name
    		        					IP      = $dns_ip
    									DNSHost = $dns_host
    									Online  = $conn     
    		        		}
    $result += $HostObj						
    }
    $result

    • Edited by Kt, Thursday, September 13, 2018 1:41 PM na
    Thursday, September 13, 2018 1:40 PM