Ideally, I would like to create an object from ipconfig that allows us to drilldown to each adapter's attributes like this: $ip.$lan.$mac for the lan adapter's mac address.
To start, I would like to develop a way to capture these 3 groups (adapter type, adapter name, adapter attributes) per adapter match into Powershell variables (objects are preferred): https://regex101.com/r/wZ3sV1/1
Here are some ideas to capture three parts of the Ethernet adapter section, but they are only capturing "Ethernet adapter Local Area Connection:":
$ip = ipconfig /all
$ip_lan = $ip | Select-String -pattern "(Ethernet [^a]*adapter) (Local[^:]+):\s*(([^\n]+\n)*)" -AllMatches | Foreach {$_.Matches} | ForEach-Object {$_.Value}
$regex_lan = [regex]"(Ethernet [^a]*adapter) (Local[^:]+):\n*(( +[^\n]+\n)*)"
$regex_lan.Matches($ip)
$regex_lan.Matches($ip).value
Also, is there a way to capture the name of the group extraction like this?:
Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller
becomes Description = Realtek PCIe GBE Family Controller
Another approach, if you want (1) an object and (2) something that should work in older versions of PowerShell, is to pull the network information using WMI:
You can use the following to explore the full set of data that you get per adapter:
Looking at the output of IPCONFIG /ALL, it looks like there are 3 different types of data being returned - the header section at the beginning and the an arbitrary number of sections for active and inactive adapters, each with a different format and set of data returned.
Here is a set of regular expressions that can be used to parse each one, and capture the values presented:
chew on that a bit, and let me know if that's making any sense.
If you want to pull data out of strings (with regexes) and assign to variables, I think what you are looking for are named capture groups, which you can use in a manner similar to the following:
Then you can pull these out of the
$matches
hashtable by name, for example:You can have multiple named capture groups in the same regex, but each one will need to have distinct names. Armed with this you should be able to match the various bits of text you want and give them names to be picked up easily afterwards.
The following blog talks about this concept a little more:
I know this doesn't directly answer your question. But rather than parsing the output of
ipconfig
with Regex. You could instead useGet-NetIPConfiguration
to get a powershell object that is easier to deal with.Thus you can do the following to get the values you are looking to acquire.
To get the MAC the address you can use the
Get-NetAdapter
cmdlet.You can correlate the two pieces of information using the InterfaceIndex. Then return a hashtable that makes each accessible. The following creates an array of these combined objects.