1

I have a hashtable of IP connections that are associated to their Destination Prefixes. Here is the code to gather it all together:

function Get-InterfaceRoutes {

    $interfaceIPs = Get-NetIPConfiguration -Detailed |
     Where-Object { $_.netadapter.status -eq 'up' } | Get-NetIPAddress -AddressFamily IPv4 | 
     Select-Object -Property IPAddress, InterfaceIndex, InterfaceAlias

    Foreach ($interfaceIP in $interfaceIPs) {
        $route = Get-NetRoute -InterfaceIndex ($interfaceIP.InterfaceIndex) | 
         Select-Object -Property ifINdex, DestinationPrefix, NextHop, RouteMetric, ifMetric | 
         Where-Object -Property DestinationPrefix -like '*.*.*.*' | Sort-Object -Property ifIndex

        [PSCustomObject]@{
            Index             = ($interfaceIp.InterfaceIndex)
            Address           = ($interfaceIP.IPAddress)
            Alias             = ($interfaceIP.InterfaceAlias)
            DestinationPrefix = ($route.DestinationPrefix)
            NextHop           = ($route.NextHop)
            RouteMetric       = ($route.RouteMetric)
            InterfaceMetric   = ($route.InterfaceMetric)
        }
    }
}
$collection = @(Get-InterfaceRoutes)

I am building a UI in PS-5.1(WinForms) to list the various indexes and their properties. With it I have this button that I want to be able to select one of the listed Destination Prefixes (of which there will be at least 1, at most n to choose from) associated with each index (again, 1-n):

$destinationSelectButton.Add_Click({

        $selectedDestination = $collection.keys | 
         Out-GridView -Title "Select Destination Prefix" -PassThru | 
         ForEach-Object { $_.Values } | Select-Object -Property DestinationPrefix
    
        Write-Host $selectedDestination | Out-String #<<<exists for confirmation in console, ignore.
    })

The problem I have with this snippet specifically is that when I select the button, I don't get the GridView box to select from the list of Prefixes. Just nothing. No error message, no window opening, just an acknowledgement in my terminal that the button was clicked.

If I arrange the code any other way, such as:

        $selectedDestination = $collection |
         Out-Gridview -Title "Select Destination Prefix" -PassThru | 
         Select-Object -Property DestinationPrefix

I get this: Hastable with values gathered as one object

Here the Destination Prefix is gathered as one object, but I want to display that array broken apart so that one can be selected from the list and sent to $selectedDestination for use later on. I suspect the code block I shared for the button SHOULD do just that, but without the window opening, and no error to say why, I am not sure where to go from here to get it to work.

Nick Keele
  • 35
  • 5
  • Well, the first problem I see is that you think you have a hashtable, but you explicitly output `[PSCustomObject]`s in your function. That's why `$collection.keys` does not work. The rest is that you need to have an inner loop to iterate all of your routes, but Santiago's answer will help you with that (he uses his last `Select-Object` as the loop to iterate the routes per adapter). – TheMadTechnician Jun 07 '22 at 00:21
  • @TheMadTechnician Ok. So that was an unknown source of confusion, I had originally thought PSCustomObject and hashtable are kind of interchangeable. But with that understanding, and Santiago's answer, I managed to get what I needed! Thanks! – Nick Keele Jun 07 '22 at 04:25

1 Answers1

1

If I understand correctly, you're just needing to loop through each object resulted from Get-NetRoute and then combine / merge that output with the result of Get-NetIPConfiguration, instead of merging all objects into one object.

For that you can use Select-Object with calculated properties:

$interfaceIPs = Get-NetIPConfiguration -Detailed |
    Where-Object { $_.NetAdapter.Status -eq 'up' } |
        Get-NetIPAddress -AddressFamily IPv4

$collection = foreach($interfaceIP in $interfaceIPs) {
    Get-NetRoute -InterfaceIndex $interfaceIP.InterfaceIndex |
        Where-Object -Property DestinationPrefix -like '*.*.*.*' |
        Sort-Object -Property ifIndex | Select-Object @(
            @{ N = 'Index';   E = { $interfaceIp.InterfaceIndex }}
            @{ N = 'Address'; E = { $interfaceIP.IPAddress }}
            @{ N = 'Alias';   E = { $interfaceIP.InterfaceAlias }}
            'DestinationPrefix'
            'NextHop'
            'RouteMetric'
            'InterfaceMetric'
        )
}

$selection = $collection | Out-GridView -PassThru
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    That worked! I only need to make adjustments now in other areas to change the iteration so that I don't have so much repetition, but my GridView is exactly what I needed! Thank you! – Nick Keele Jun 07 '22 at 04:26