我有一個與其目標前綴相關聯的 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
我明白了:

在這里,目標前綴被收集為一個物件,但我想將該陣列分開顯示,以便可以從串列中選擇一個并將其發送到 $selectedDestination 以供以后使用。我懷疑我為按鈕共享的代碼塊應該這樣做,但沒有打開視窗,也沒有錯誤說明原因,我不確定從這里到哪里讓它作業。
uj5u.com熱心網友回復:
如果我理解正確,您只需要遍歷每個物件Get-NetRoute,然后將該輸出與結果合并/合并,Get-NetIPConfiguration而不是將所有物件合并為一個物件。
為此,您可以使用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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487522.html
標籤:数组 电源外壳 哈希表 powershell-5.1 外网格视图
上一篇:目錄串列內遞回父子
下一篇:如何回傳閉包或處理不回傳閉包
