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