从PowerShell哈希表中查找并检索关联值
输出:从PowerShell哈希表中查找并检索关联值,powershell,Powershell,输出: $netshGet = "netsh.exe int ipv4 sh int" 在我的脚本中,我知道接口的名称是“Named-interface-1” 我需要知道和从这个表中检索的是它关联的“Idx”值。最终,我希望值位于变量中,因此变量中唯一的内容是“19”,即: 有没有一种方法可以在对象{$\.Name-eq'Named-Interface-1'}所在的表中搜索Idx值?这个正则表达式似乎适合我 Write-Host $idx 19 您需要做的就是修改$AdapterName变量
$netshGet = "netsh.exe int ipv4 sh int"
在我的脚本中,我知道接口的名称是“Named-interface-1”
我需要知道和从这个表中检索的是它关联的“Idx”值。最终,我希望值位于变量中,因此变量中唯一的内容是“19”,即:
有没有一种方法可以在对象{$\.Name-eq'Named-Interface-1'}所在的表中搜索Idx值?这个正则表达式似乎适合我
Write-Host $idx
19
您需要做的就是修改
$AdapterName
变量的值,然后运行代码。您只需要从netsh.exe返回一个数组a字符串。你需要为你的数据解析文本。太棒了,这工作太完美了!感谢您提供完整的代码和示例。
Write-Host $idx
19
$AdapterName = 'Wi-fi';
$output = (netsh.exe int ipv4 sh int) -join "`n";
([Regex]"(?<idx>\d+)(?=.*$AdapterName)").matches($output)[0].Value
Idx Met MTU State Name
--- ---------- ---------- ------------ ---------------------------
3 5 1500 disconnected Ethernet
1 50 4294967295 connected Loopback Pseudo-Interface 1
11 25 1500 connected Wi-fi
12 5 1500 disconnected Local Area Connection* 2