arrayspowershellhashtablepowershell-5.1out-gridview

How to Out-Gridview multiple values to one key in Powershell?


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.


Solution

  • 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