none
Batch File to Ping Multiple PCs and Writing Text to File. RRS feed

  • Question

  • I'm pretty new to this so please bear with me, and if you require anymore information from me please say. Thanks in advance for your help.

    I have this code that pings different PCs then returns back if they are online/offline. I wanted to know if you could add another column once the bat file has ran its ping test so it has the computer name next to it.

    @echo off 
    if exist C:\tools\computers.txt goto Label1 
    echo. 
    echo Cannot find C:\tools\computers.txt 
    echo. 
    Pause
    goto :eof
    
    :Label1 echo PingTest executed on %date% at %time% > C:\tools\z.txt echo =========== >> C:\tools\z.txt 
    for /f %%i in (C:\tools\computers.txt) do call :Sub %%i notepad C:\tools\z.txt 
    goto :eof
    
    :Sub echo Testing %1 
    set state=alive 
    ping -n 1 %1 | find /i "bytes=" || set state=dead 
    echo %1 is %state% >> C:\tools\z.txt

    The bat file creates a document that shows the following;

    PingTest executed on 28/07/2016 at 13:10:28

    99.1.82.28 is alive

    99.1.82.100 is alive

    ect.


    If possible I would like the bat file to run so it displays this;

    The bat file creates a document that shows the following;

    PingTest executed on 28/07/2016 at 13:10:28

    Computer 1 : 99.1.82.28 is alive

    Computer 2 : 99.1.82.100 is alive

    ect.

    --

    Would appreciate any help & guidance on this.

    Thanks.

    Thursday, July 28, 2016 12:25 PM

Answers

  • Hi,

    well first of all you could jettison batch. It's not really the most modern information retrieval tool available.

    Let's look at how this can be done in PowerShell:

    Get-Content "C:\Folder\Computers.txt" | ForEach-Object {
    	New-Object PSObject -Property @{
    		ComputerName = $_
    		Alive = (Test-Connection -ComputerName $_ -Count 1 -Quiet)
    		Address = ([System.Net.Dns]::GetHostEntry($_)).AddressList[0]
    	}
    } | Export-Csv "C:\Folder\Stats.csv" -NoTypeInformation

    Not only is it simpler and easier to read ... the output is a neat Csv file you can open in Excel for extra user-comfort.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, July 28, 2016 12:34 PM