It's very common as IT Pro want to know which is the name of a specific ip address that found in your network. You can use old techniques like nslookup from command but Today i will explain how can resolve ip address to hostname with the powershell.
You can download the final version of this small script with GUI from here
or you can Read all the series until the final version
How to scan ip addresses range and get important details with Powershell GUI
How to scan ip addresses range and get important details with Powershell GUI - Part 2
How to scan ip addresses range and get important details with Powershell GUI - Part 3
How to scan ip addresses range and get important details with Powershell GUI - Part 4
My scope of this article is to create a small but useful Asset Management System that can help you to know what hardware you have in your environment anytime that you want.
You can find lot of Asset Management Systems in the Internet with lot of features but what if i want to have something free or no expensive with the settings that i need ?
So let's start with the first few lines
- Open Powershell ISE to write the script
- First line will use the Get-Content to get the content of the txt file that will write ip addresses that want to scan and save it in variable that will use it in loop
$ipaddress=Get-Content -Path C:\powershell-reports\ip.txt
- Now i will create an array that will save the results.
$results = @()
- The most important now is to use a loop that can pass all the lines of the file that i wrote the ip addresses.
- Inside the loop i create an object and add a hostname as property in object that will present the value of the command that will resolve the ip ddress to hostname from yoru DNS Server [System.Net.Dns]::GetHostByAddress($i).HostName
ForEach ($i in $ipaddress)
{
$o=new-object psobject
$o | Add-Member -MemberType NoteProperty -Name hostname -Value ([System.Net.Dns]::GetHostByAddress($i).HostName)
$results +=$o
}
- The last line will use it to print in monitor the results from the object or export to csv.
$results | Select-Object -Property hostname | Export-Csv C:\powershell-reports\machinenames.csv
- Let's combine all the lines of the code and run the script
$ipaddress=Get-Content -Path C:\powershell-reports\ip.txt
$results = @()
ForEach ($i in $ipaddress)
{
$o=new-object psobject$o | Add-Member -MemberType NoteProperty -Name hostname -Value ([System.Net.Dns]::GetHostByAddress($i).HostName)
$results +=$o
}$results | Select-Object -Property hostname | Export-Csv C:\powershell-reports\machinenames.csv
I hope to find useful this single script and use it with your requirements in your environment. I will continue in next months with articles to keep up to date with my progress.
I invite you to follow me on Twitter , Google+ or Facebook. If you have any questions, send email to me at info@askme4tech.com.
Have a nice weekend!!