Ask a questionAsk a question
 

Proposed AnswerPinging a list of Hostnames

  • Thursday, November 05, 2009 9:15 PMNick Colyer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm wondering if someone can help me here. I'm very new to scripting and powershell in itself.

    I'm looking to create a powershell script that does that following:

    1. Takes a .txt file which has a list of hostnames
    2. Ping each host name, but the host could be on any of 3-4 domains..

    so if the Host is called Host1

    I need to ping

    host1.domain1.com
    host1.domain2.com
    host1.domain3.com

    If the host responds on any of the 3 domain suffixes, it reports true.
    If the host does not respond at all, then it reports false.

    This data i then want output to a spreadsheet.

    In the spreadsheet, I simply want it to have 2 columns

    Column A has the hostname
    column B is either True or False, depending if the server responding to any ping request on any of the domains.

    I hope that makes sense.

    I have over 400 servers to ping.

    Thanks

    Nick

All Replies

  • Thursday, November 05, 2009 10:16 PMGunner999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    Don't need powershell my friend, you can do this from a batch file easier.

    To get data into excel from a script format all output as comma separated values (CSV), or tab/space separated values (TSV or TXT) files.

    The batch file below will take a text file of you 400 servers, and output a CSV file for import into Excel.  You can modify the output as you want.  The harder part will be for you to get the 400 server names into a text file for input.

    Sample Output
    Success,server1
    Failure,server2


    Save file as MultiPing.bat
    @Echo Off

    REM   Author: Gunner999
    REM  Created: 2004-02-12
    REM Modified: 2009-10-21
    REM  Purpose: Universal batch file to ping multiple devices on the network.
    REM            
    REM Input Variables: %1 is a file name containing the device Netbios name,
    REM                      DNS name, or the ip address.  One item per line.
    REM                                

    If '%1'=='' GOTO Syntax


    For /F %%i in (%1) do Call :StartPing %%i
    Goto End

    :StartPing
    PING %1 -n 1 | FIND "TTL" > NUL
    IF NOT ERRORLEVEL 1 GOTO :Success
    PING %1-n 1 -w 400 | FIND "TTL" > NUL
    IF ERRORLEVEL 1 Goto Errormsg

    :Success
    Echo Success,%1
    Goto :EOF

    :ErrorMsg
    Echo FAILURE,%1
    Goto :EOF


    :Syntax
    Echo.
    Echo Syntax:
    Echo.
    Echo MultiPing Filename
    Echo.
    Echo Examples:
    Echo MultiPing Servers.txt
    Echo.
    Echo Press CTRL-C to quit.
    Echo.
    pause


    :End

    • Proposed As Answer byGunner999 Saturday, November 07, 2009 4:46 PM
    •  
  • Thursday, November 05, 2009 10:33 PMmjolinor Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    See if this is close to what you need:

    $outfile = "pingresult.csv"
    $servernames = gc server_names.txt

    $domains = "domain1.com","domain2.com","domain3.com"


    [int]$timeout = 200
    [int]$TTL = 128
    [switch]$DontFragment = $true
    [int]$buffersize = 32

    $options = new-object system.net.networkinformation.pingoptions
    $options.TTL = $TTL
    $options.DontFragment = $DontFragment
    $buffer=([system.text.encoding]::ASCII).getbytes("a"*$buffersize)

    $pingresults = @()
    foreach ($servername in $servernames ){
     $pingresult = "" | select Server,Ping
     $pingresult.server = $servername
     $pingresult.ping = "False"
     
     foreach ($domain in $domains){
     
     $dnsname = $servername + "." + $domain
     Write-Host "pinging $($dnsname)"
     $ping = new-object system.net.networkinformation.ping
     $reply = ""
     $reply = $ping.Send($DNSname,$timeout,$buffer,$options)
     if ($reply.status -eq "Success"){$pingresult.ping = "True" }
     }
     
     $pingresults += $pingresult
    }
    $pingresults | Export-Csv $outfile -notype

    • Proposed As Answer byGunner999 Saturday, November 07, 2009 4:46 PM
    •  
  • Sunday, November 08, 2009 3:26 PMNick Colyer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks everyone, that was a great help!