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
Add to: | Technorati | Digg | del.icio.us | Yahoo | BlinkList | Spurl | reddit | Furl |
Like this:
Like Loading...