Get-QADComputer
can accept lots of forms of computer identification: name, DNS name, DN, and so on, but IP address is currently not there. Fear not! PowerShell and .NET can do wonders.
With a little bit of help from Jeffrey, I scripted a simple function which turns IP addresses into computer names and supports every possible scenario I could think of:
# No parameters PS C:\> Get-ComputerNameByIP Please supply the IP Address: 10.20.30.40 mycomp.domain.local # With parameter PS C:\> Get-ComputerNameByIP 10.20.30.40 mycomp.domain.local # With pipeline PS C:\> '10.20.30.40', '10.20.30.41' | Get-ComputerNameByIP mycomp.domain.local myserv.domain.local # Or reading from a text file (one IP address per line) PS C:\> get-content C:\temp\ip_addresses.txt | Get-ComputerNameByIP mycomp.domain.local myserv.domain.local # Or piping that further to get AD computer objects PS C:\> '10.20.30.40', '10.20.30.41' | Get-ComputerNameByIP | ForEach-Object { Get-QADComputer -DnsName $_ } Name Type DN ---- ---- -- MYCOMP computer CN=MYCOMP,OU=Redmond,OU=US,OU=Desktops... MYSERV computer CN=MYSERV,OU=Redmond,OU=US,OU=Servers,...
And here’s the actual function which you can copy/paste into your PowerShell window, add to your PowerShell profile, or “dot-source” from an external file.
function Get-ComputerNameByIP { param( $IPAddress = $null ) BEGIN { } PROCESS { if ($IPAddress -and $_) { throw 'Please use either pipeline or input parameter' break } elseif ($IPAddress) { ([System.Net.Dns]::GetHostbyAddress($IPAddress)).HostName } elseif ($_) { [System.Net.Dns]::GetHostbyAddress($_).HostName } else { $IPAddress = Read-Host "Please supply the IP Address" [System.Net.Dns]::GetHostbyAddress($IPAddress).HostName } } END { } }
Have fun!
Tags: AD, AD cmdlets, Active Directory, PowerShell, desktop management
Hello,
Great script, but i would like to be able to run this to gather all computers on my subnet and pipe it to a file that list:
Name Type DN IPADDRESS
MYSERV computer CN=MYSERV,OU=Redmond,OU=US,OU=Servers, 10.10.10.1
Could you help me out on this one?
I did not make this clear, but i would like to give the script a range. IE. 10.10.20.1 to 10.10.20.255 and for it to list on that subnet to a file..
Mike, here’s how I would supply the range:
10..15 | ForEach-Object {“10.20.30.$_”} | Get-ComputerNameByIP
So I am basically supplying the actual range in the beginning and the constant part in the second (foreach) section putting the variable part inside the string.
However, because the range might contain invalid IP addresses I would add some error handling to this. Here’s the updated function with error handling (I have also removed .HostName so full objects with IP addresses are returned – not just the names):
function Get-ComputerNameByIP {
param(
$IPAddress = $null
)
BEGIN {
}
PROCESS {
if ($IPAddress -and $_) {
throw ‘Please use either pipeline or input parameter’
break
} elseif ($IPAddress) {
([System.Net.Dns]::GetHostbyAddress($IPAddress))
} elseif ($_) {
trap [Exception] {
write-warning $_.Exception.Message
continue;
}
[System.Net.Dns]::GetHostbyAddress($_)
} else {
$IPAddress = Read-Host “Please supply the IP Address”
[System.Net.Dns]::GetHostbyAddress($IPAddress)
}
}
END {
}
}
Thanks for the article. I don’t know why this script not running on my computer for some IP Addresses.
Probably DNS lookups failing. What do you get if you do this for some of the failing addresses?
[System.Net.Dns]::GetHostbyAddress( ‘10.20.30.40’ )
Hi Dmitry, is there way to add functionality to do lookups against a particular DNS server? I didn’t seem to see that available in the .Net DnsClass…
GW, frankly, I don’t know. As you can see I am just using the .NET System.Net.DNS class, and I don’t know whether it is possible to make it work against a particular server. The MSDN documentation does not seem to provide an answer: http://msdn.microsoft.com/en-us/library/system.net.dns.gethostbyaddress.aspx
Dmitry,
Is there a way to have a script read a txt file that contains a list of IP address and then returns the IP address and the computer name and formated like below:
IP ComputerName
10.1.1.1 MikesComputer
10.1.1.2 FranksDevBox
thank you for your help
Dean,
Sure, suppose you have a text file with IP addresses, one at each line:
get-content IPAddresses.txt | ForEach-Object {
[System.Net.Dns]::GetHostbyAddress($_) |
Add-Member -Name IP -Value $_ -MemberType NoteProperty -PassThru
} | Select IP, HostName | Export-CSV IPandName.csv
To get faster response in the future try asking such questions in our forums: http://www.powergui.org/forumindex.jspa?categoryID=55
Dmitry
where would error handling be placed if the IP is not attached to anything (not on the wire)
So.. how can I work this process backwards.. I have server in a DMZ and need their IP addresses .. How can I supply a server name and use .NET to return the IP address..?
Been trying to use System.Net.NetworkInformation.Ping but it reurns the IP as a combined number of some sort…??
Thx
So.. how can I work this process backwards.. I have server in a DMZ and need their IP addresses .. How can I supply a server name and use .NET to return the IP address..?
Been trying to use System.Net.NetworkInformation.Ping but it reurns the IP as a combined number of some sort…??
Thx
Hello John,
backward function could be:
function Get-ComputerIPByName {
param(
$ComputerNames = $null
)
BEGIN {
}
PROCESS {
if ($ComputerNames -and $_) {
throw ‘Please use either pipeline or input parameter’
break
} elseif ($ComputerNames) {
([System.Net.Dns]::GetHostbyName($ComputerNames))
} elseif ($_) {
trap [Exception] {
write-warning $_.Exception.Message
continue;
}
[System.Net.Dns]::GetHostbyName($_)
} else {
$ComputerNames = Read-Host “Please supply the computer names”
[System.Net.Dns]::GetHostbyAddress($ComputerNames)
}
}
END {
}
}
And thank you very much Dmitry 🙂
Hi! I’ve been reading your web site for a long time now and finally got the courage to go ahead and give you a shout out from Lubbock Texas! Just wanted to say keep up the great work!
Hi Dimi,
finally >13 years after you have written your funbtion, I’ve finally found it.
Only sad thing is that it is not working with the latest Exchange Server OR in my enviroment.
Any new version of it?