1

I have a few IP addresses on excel file, I want to do nslookup. How can I do it on excel or powershell? Any ideas or recommendations. Don't confuse yourself with vlookup. I am asking for nslookup in command prompt. Is there any way to look for every single ip address from each row and find the domain. If there is a way of doing it using VBA in excel, that will be great.

See the attached to clarify the requirement. Feel free to ask me any questions.

enter image description here

lisa_rao007
  • 347
  • 1
  • 5
  • 23
  • 1
    Use Import-Csv and Resolve-DnsName cmdlets – Scepticalist Feb 06 '20 at 06:19
  • Do me a favor, make sure the Developer tab in Excel is visible (settings), next Click Record Macro, do some actions in Excel, then click Stop Recording. Now Alt+F11 and see the Module that was created by recording your actions. Now you know how to generaet code using actions and you will be able to work out how to use my code :) – Jeremy Thompson Feb 06 '20 at 23:08

2 Answers2

2

Try this in powershell

Import-Csv "Csvpath" | ForEach {
    $IPAddress = $_
    $hostname = ([System.Net.DNS]::GetHostByAddress($_)).hostname
    Write-Host "$($IPAddress) $($Hostname)"
}
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Is this working for you. $IpAddress = what's going in here. Do I need to write row1 or something like this. How is it going to know which row or column. I have more than 10, almost 1000 ips – lisa_rao007 Feb 06 '20 at 13:14
  • I get an error saying " Cannot find an overload for "GetHostByAddress" and the argument count: "1". then my drive location and $hostname = ([System.Net.DNS]::GetHostByAddress($_)).hostname – lisa_rao007 Feb 06 '20 at 13:43
1

This is for PowerShell.

If you save the data in the Excel file as CSV or in a text file where each IP address is listed on its own line, the following should work.

The below code assumes a text file with IP addresses:

$result = Get-Content -Path 'D:\IpAddresses.txt' | ForEach-Object {
    $dnsRecord = Resolve-DnsName -Name $_ -ErrorAction SilentlyContinue
    $hostName  = if ($dnsRecord) { $dnsRecord.NameHost } else { "Not found" }
    [PsCustomObject]@{
        'IPAddress' = $_
        'Domain'    = $hostName
    }
}

# output on console
$result

# write results to new CSV file to easily open in Excel
$result | Export-Csv -Path 'D:\IP_Domain.csv' -UseCulture -NoTypeInformation

Output:

IPAddress      Domain                                       
---------      ------                                       
208.97.177.124 apache2-argon.william-floyd.dreamhost.com    
208.97.177.125 apache2-linus.john-hart.dreamhost.com        
208.97.177.126 apache2-daisy.charles-carroll.dreamhost.com  
208.97.177.127 apache2-igloo.benjamin-harrison.dreamhost.com
208.97.177.128 apache2-emu.charles-carroll.dreamhost.com    
208.97.177.129 apache2-rank.thomas-heyward-jr.dreamhost.com 
208.97.177.130 apache2-udder.arthur-middleton.dreamhost.com 
208.97.177.131 apache2-bongo.william-ellery.dreamhost.com   
208.97.177.132 apache2-bongo.william-hooper.dreamhost.com


If you save the Excel file as CSV (which is MUCH easier to handle in PowerShell than an Excel file) something like this:
IpAddress
208.97.177.124
208.97.177.125
208.97.177.126
208.97.177.127
208.97.177.128
208.97.177.129
208.97.177.130
208.97.177.131
208.97.177.132

Then all you need to change in the code is the top line into:

# read the CSV file and select an array of the IP addresses listen in column 'IpAddress'
$result = Import-Csv -Path 'D:\IpAddresses.csv' | Select-Object -ExpandProperty IpAddress | ForEach-Object {
    # rest of the code
}

Or shorter:

$result = (Import-Csv -Path 'D:\IpAddresses.csv').IpAddress | ForEach-Object {
    # rest of the code
}

You can of course also read directly from Excel, but that takes a lot more doing:

# open the Excel file and read the values into an array:
$file      = 'D:\test.xlsx'
$row       = 1               # the row number you want to read. Numbering starts at 1

# create an Excel COM object and open the file
$excel         = New-Object -ComObject Excel.Application
$excel.Visible = $false
$workbook      = $excel.Workbooks.Open($file)
$sheet         = $workbook.Worksheets(1).Activate

# get the total number of columns in the sheet
$colCount = ($sheet.UsedRange.Columns).Count
# collect the cell values in a loop
$addresses = for ($col = 1; $col -le $colCount; $col++) {
    $sheet.Cells.Item($row, $col).Text
}

# close excel and clean up the used COM objects
$excel.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

Then loop through the addresses using

$result = $addresses | ForEach-Object {
    # rest of the code
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • @user3713336 The code used a text file as example. If you save the Excel data as CSV file, only the first line needs to be altered. I have added code for that too. – Theo Feb 06 '20 at 14:22
  • I have changed it and for the output file, Do I need to create a output file and leave it blank or it will create it. – lisa_rao007 Feb 06 '20 at 14:40
  • @user3713336 The final `Export-Csv` cmdlet will create the output file for you. All you have to do is change the path and name to somewhere on your computer where you can find it. – Theo Feb 06 '20 at 14:44
  • @user3713336 It simply means you have nothing in your input file (be it .TXT, .CSV or Excel). Please think it over, try the CSV example I gave first. Which version of my answer did you try? Did you set the filenames (all just examples) to the paths and filenames of the REAL files? – Theo Feb 06 '20 at 14:58
  • It's not empty now it just run but doesn't output anything... – lisa_rao007 Feb 06 '20 at 15:30
  • @user3713336 That is because you are not **running** anything... As said, this is Powershell code. Start the .ps1 file with powershell.exe. Don't expect it to run like an executable. It's a script after all. Please first google for what a scripting language like powershell is and how to execute a script. Then lookup some of the very basic of the language. – Theo Feb 06 '20 at 18:21
  • @user3713336 First think of what input file you want to use. If **text** then copy/paste the first code i gave in the ISE editor an run. If **CSV** then do what I've explained in the second part. If **Excel** then copy/paste the third code. In all 3 cases, what is inside the loop is the same stuff, so there is no need to change that. – Theo Feb 06 '20 at 19:23
  • @user3713336 You have pasted the code OK I see. Now simpy press the `F5` key or click the green `play` button. I can see you are typing some command in the direct window (blue) to hopefully start a file that does not exist. Just look closely to what you have typed to see the mistakes. – Theo Feb 06 '20 at 19:44
  • @user3713336 I'm so glad to hear that. I would be very grateful if you would mark my answer accepted by clicking on the faint checkmark icon next to it. – Theo Feb 06 '20 at 19:54
  • Can you please mark my question one up or shows research helpful, so it will help me asking more questions in the future. Thank you – lisa_rao007 Feb 20 '20 at 02:46