Pull IP address from file and output Computer Name

I have a file that includes IP addresses of all PC\'s on our network. I need to pull the IP address from the file and run a command to output the IP address and the Computer Name. Here is what I\'ve come up with and this works if the file only has the IP address. My file has more than one column of information? Please help.

@echo off
set IPList=C:\temp\list.txt
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%IPList%") do (
for /f "tokens=2 delims= " %%B in ('ping -a %%A -n 1 -w 500 ^|findstr Pinging') do (
set PCName=%%B
echo %%A,!PCName:~0,-1!>>final.csv
))
1 answer Last reply
More about pull address file output computer name
  1. Use VBscript it's much more flexible as it lets you open the file and read it with more control.

    Here is a similar script I wrote to delete computers from AD automatically, it reads a spreadsheet to get the names. You can just use the code needed to read in data, and write different code to manipulate it using the call function.

    The r=2 is what sets the Row it will read. If you play around with that you will bea ble to read whichever row or rows you need.


    '**** Delete old and unused computer accounts from Active Directory
    '**** Reads computer names from an Excel file
    '**** Author: ME!
    '**** 1/18/08

    On Error Resume Next

    '**** Define some constants ****
    Const FOR_READING = 1
    Const ForAppending = 8
    Dim CompName
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    '**** Set Excel object and path to spreadsheet containing PC names ****
    Set objExcel = WScript.CreateObject ("excel.application")
    objExcel.DisplayAlerts = 0
    Set PathExcel = objExcel.WorkBooks.Open _
    ("C:\Scripts\DeleteOldPC\AD365.xls", False, True)

    r = 2
    Do until len(objExcel.cells(r, 1).value) = 0


    '**** Set variable for computer name to ping from row r, column 1 of spreadsheet ****
    strComputer = objExcel.cells(r, 1).Value

    Set objShell = CreateObject("WScript.Shell")
    'WScript.Echo strComputer
    objExec = objShell.Run("ping -n 2 -w 2000 " & strComputer, 7, True)

    If objExec = 0 Then
    Call PCExists
    Else
    Call DeleteFromAD
    End If

    r=r+1
    Loop

    WScript.Echo "Script Completed"
    WScript.Quit

    '**** Skip deleting the computer account if the machine is pingable ****
    Sub PCExists()
    '**** Write computer skipped to log file ****
    Set Results= objFSO.OpenTextFile _
    ("C:\Scripts\DeleteOldPC\Delete_From_AD_Results.csv", ForAppending, True)
    Results.Write strComputer & ", Not Deleted From AD At , " & Now() & vbCrLf
    Results.Close
    End Sub

    '**** Delete computer account from AD if machine is not pingable ****
    Sub DeleteFromAD()
    Set objComputer = GetObject("LDAP://CN=" & strComputer & _
    ",CN=Computers,DC=umassmemorial,DC=org")
    objComputer.DeleteObject (0)

    '**** Write success to log file ****
    Set Results= objFSO.OpenTextFile _
    ("C:\Scripts\DeleteOldPC\Delete_From_AD_Results.csv", ForAppending, True)
    Results.Write strComputer & ", Deleted From AD At , " & Now() & vbCrLf
    Results.Close
    End Sub
Ask a new question

Read More

IP Address Computers Command Prompt Windows XP