0

I am trying to filter the results of running the netsh command to retrieve a certain value.

PS C:\windows\system32> netsh interface ipv4 show interfaces

Idx     Met         MTU          State                Name
---  ----------  ----------  ------------  ---------------------------
  1          50  4294967295  connected     Loopback Pseudo-Interface 1
 12          25        1500  connected     Wireless Network Connection
 11          10        1500  connected     Local Area Connection
 14          40        1500  disconnected  Bluetooth Network Connection
 21          20        1500  connected     VMware Network Adapter VMnet1
 22          20        1500  connected     VMware Network Adapter VMnet8

I want to retrieve the Idx value of the Local Area Connection.

Piping the above netsh command to Where-Object doesnt seem to work.

Assign a variable to the netsh command above to treat it as an object doesnt seem to work either.

How can I achieve this?

Thanks!

Based on below answers and playing around this is the solution I came up with, not pretty, but it works. I have to wonder if there is a .NET equivalent that I can use, but Ill save that for a later date. Thanks for the help.

$interfaces = netsh interface ipv4 show interfaces

foreach ($i in $interfaces) 
{
    if ($i -match 'Local Area Connection') {
    $indexvalue = $i.Substring(0,6)
    $indexvalue = $indexvalue.Trim()
    Write-Host $indexvalue
    }
}
Jim P.
  • 1,087
  • 3
  • 9
  • 24
  • You can achieve it by piping the output to `Select-Object` to skip the first 2 lines, then to `Where` to perform a regex match, and finally to `ForEach-Object` to extrapolate the data from the automatic `$Matches` variable. That's how I'd do it at least. – TheMadTechnician Dec 08 '15 at 17:53
  • @TheMadTechnician - I tried this command, and it runs without error, but it doesnt return any results. I wasnt sure how to implement the ForEach-Object, but wasn't sure how to use it in this scenario. Command Run: netsh interface ipv4 show interfaces | Select-Object -Skip 2 | Where {$_.Name -eq 'Local Area Connection'} – Jim P. Dec 08 '15 at 18:19

1 Answers1

2

Well, you at least showed (in a comment, you should have updated the question, but we'll let that slide) that you did write code and you did try. So, let's get you what you want...

$IDX = netsh interface ipv4 show interfaces | Where {$_ -match "(\d+).*?Local Area Connection"}|ForEach{$Matches[1]}

That gets you just what you want. What I would personally do would be to get objects for each entry in case you wanted to use other records later, or other info later...

$Connections = netsh interface ipv4 show interfaces | Select-Object -Skip 2 | Where {$_ -match "(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)$"} | ForEach{
[PSCustomObject]@{
    'Idx'=$Matches[1]
    'Met'=$Matches[2]
    'MTU'=$Matches[3]
    'State'=$Matches[4]
    'Name'=$Matches[5]
}}

Then you have an array of objects, and each object will have 5 properties to match up with the table that is output by netsh. For more info on the RegEx match check out the explanation at www.RegEx101.com. Then if you wanted the IDX for the Local Area Connection you would do something like:

$Connections |Where{$_.Name -eq 'Local Area Connection'}|Select -Expand IDX

I admit it's overkill for what you're doing, but working with objects instead of strings can be useful down the line, and understanding this approach allows you to accomplish so many more things in the future.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Thanks for expanding on that on how to essentially turn these into objects to work with, which is much cleaner, and exponentially more powerful, exactly what I was looking for! Thanks again – Jim P. Dec 08 '15 at 21:31