Getting a computer's IP address - am I making this too complicated?

I've been asked to resolve a bunch of machine names to IP addresses, and the person wanting the results doesn't care if the machines are actually available or not. I figured I could try the following methods:

Ping, WMI, parsing AD

so I put together the following script:

function Get-IPAddress{

Param([string]$computername=$env:computername)

[regex]$ip4="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled='True'" -computer $computername | 
 Select DNSHostname,Index,Description,@{Name="IPv4";Expression={ $_.IPAddress -match $ip4}},
 @{Name="IPv6";Expression={ $_.IPAddress -notmatch $ip4}},MACAddress
 }

$_csv         = @()
$_csv_header  = """IP"",""Server"""
$_csv         += $_csv_header

$servers = get-content C:\temp\servers.txt
foreach ($server in $servers){
    If (Test-Connection $server -Count 1 -Quiet -ErrorAction SilentlyContinue ) {
        $ipaddress = get-ipaddress $server
        $ipv4address = $ipaddress.ipv4}
        Else { $ipv4address = 'IP DOES NOT RESOLVE'}
        If ( $ipv4address -eq $null){
            $ipv4address = Get-ADComputer $server -Properties ipv4Address | ft ipv4Address,name}
            If ( $ipv4address -eq $null){
            $testconnection = test-connection $server -count 1
            $ipv4address = $testconnection.IPV4Address.IPAddressToString}
        $_csv += """$ipv4address"",""$server"""
        }
    

$_csv | Out-File c:\temp\servers.csv -Encoding ASCII

Am I making that too complicated? Is there something I overlooked? Is there a way to take error returns on failed WMI attempts ("get-wmiobject : Access is denied") and put them in my final output? Am I just dumb because someone has already written a better script?

Than

August 26th, 2014 3:13pm

Hi,

Here's how I'd do something like that:

Get-Content .\computerList.txt | ForEach {

    $computerName = $_

    $props = @{
        ComputerName = $computerName
        Status = 'OFFLINE'
        IPAddress = ''
    }
    
    If (Test-Connection -ComputerName $computerName -Count 1 -Quiet) {

        $props.Status = 'ONLINE'

        try {

            $details = [System.Net.Dns]::GetHostByName($computerName)

            $ipAddrs = $details.AddressList -join ','

            $props.IPAddress = $ipAddrs

        } catch {

            $props.IPAddress = 'ERROR'

        }

    }

    New-Object PsObject -Property $props

}

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2014 3:56pm

Hi Zarberg,

why not try using DNS for this?

[System.Net.Dns]::GetHostEntry("ComputerName") would resolve them just fine. Doesn't require Wmi access and machine running either ...

You can handle errors by using try / catch statements (Get-Help about_try_catch)

Cheers,
Fred

August 26th, 2014 3:59pm

Using WMI isn't going to help if you want the IP addresses of machines whether they are available or not.  

Better to use DNS directly:

Foreach ($server in $servers)
{  
  foreach ($IPAddress in [net.dns]::gethostentry($server).AddressList )
 {
   if ( $IPAddress.IPAddressToString -match '^[0-9.]+$' )
   { [PSCustomObject]@{IPAddress = $IPAddress;Server = $server} }
 }
}

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2014 4:11pm

If you are just trying to get a list of IP addresses from a list of computer names, I would put the computer names into a .txt file and use this script:

$computerList = Get-Content C:/PATHTOFILE/systems.txt ForEach ($computer in $computerList) { $ping = New-Object System.Net.NetworkInformation.Ping $rslt = $ping.Send($computer) $ip_address = $rslt.address.IPAddressToString "$computer,$ip_address" | Out-File -Append C:\PATHTORESULTS\ip.csv }
July 27th, 2015 4:44am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics