Wednesday, May 4, 2016

PowerShell v3 Get Active NIC Information

To quickly gather the information I needed for a script, I simply wanted to identify one (or more) IP addresses that were enabled on my machine. Using the Scripting Guys post,
Use PowerShell to Identify Your Real Network Adapter
I was able to quickly take the following command, and, get basic information for my NIC.
Get-WmiObject win32_networkadapterconfiguration -Filter ipenabled = "true"
When I run this command, I get a large chunk of information like this:
DHCPEnabled      : True
IPAddress        : {192.168.1.2}
DefaultIPGateway : {192.168.1.1}
DNSDomain        :
ServiceName      : abcexpress
Description      : Intel(R) 8123LM Gigabit Network Connection
Index            : 7
In my case, I just needed the actual IPv4 address, so, I used this approach:
(Get-WmiObject win32_networkadapterconfiguration -Filter ipenabled = "true").IPAddress |
Where-Object {$_ -as [System.Version]}
The beauty here is that you can zero in on a specific property (the results of wrapping in parentheses and using .IPAddress) then pipelining to a Where-Object cmdlet to test if the value can be typed as a [System.Version] object. Since IPv4 addresses pass as [System.Version] objects, this is a foolproof way to find an IP address without having to parse a lot of data or use complex expressions.

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger