繁体   English   中英

如何在Powershell中将多个值显示为一个键?

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

我有一个与其目标前缀相关联的 IP 连接哈希表。 这是将它们收集在一起的代码:

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)

我正在 PS-5.1(WinForms) 中构建一个 UI 来列出各种索引及其属性。 有了它,我有这个按钮,我希望能够选择与每个索引关联的列出的目标前缀之一(其中至少有 1 个,最多n 个可供选择)(再次,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.
    })

这个片段的问题是,当我选择按钮时,我没有从前缀列表中选择 GridView 框。 根本不值一提。 没有错误消息,没有打开窗口,只是在我的终端中确认单击了按钮。

如果我以其他方式安排代码,例如:

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

我明白了: Hastable 将值收集为一个对象

在这里,目标前缀作为一个对象收集,但我想显示该数组被拆分,以便可以从列表中选择一个并将其发送到 $selectedDestination 以供以后使用。 怀疑我为按钮共享的代码块应该这样做,但没有打开窗口,也没有错误说明原因,我不确定从这里到哪里让它工作。

如果我理解正确,您只需要遍历Get-NetRoute产生的每个对象,然后将该输出与Get-NetIPConfiguration的结果组合/合并,而不是将所有对象合并到一个 object中。

为此,您可以使用带有计算属性Select-Object

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM