$netshGet = "netsh.exe int ipv4 sh int"
输出:
Write-Host $netshGet.ScriptOutput
Idx Met MTU State Name
--- ------ ---------- ----------- -----------------
1 50 4294967295 connected Loopback
19 10 1500 connected Named-Interface-1
20 10 1500 connected Named-Interface-2
在我的脚本中,我知道接口的名称是“Named-Interface-1”。
我需要知道的以及我需要从此表中检索的是其关联的“Idx”值。最终我希望值在变量中,因此变量中唯一的东西是“19”,即:
Write-Host $idx
19
有没有办法在这个表中搜索Idx值Where-Object {$ _。Name -eq'Named-Interface-1'}?
答案 0 :(得分:1)
这个正则表达似乎对我有用。
$AdapterName = 'Wi-fi';
$output = (netsh.exe int ipv4 sh int) -join "`n";
([Regex]"(?<idx>\d+)(?=.*$AdapterName)").matches($output)[0].Value
以下是netsh.exe命令的输出:
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
您需要做的就是修改$AdapterName
变量的值,然后运行代码。