2013-02-21 49 views
1

玩PS和我有一个简单的脚本。创建一个前瞻性匹配

ipconfig /all | where-object {$_ -match "IPv4" -or $_ -match "Description"} 

这是伟大的,做我期望的。我想要做的事情是提前阅读,并只显示IPv4行之前的描述。或反向搜索,并获得IPV4和下一个描述,然后寻找下一个IPv4等

有没有办法做到这一点,而不是通过创建一个数组,然后通过数组旋转通过旋转解出有意义的部分?

在我的笔记本电脑结果

此命令:

Description . . . . . . . . . . . : Microsoft Virtual WiFi Miniport Adapter 
Description . . . . . . . . . . . : Killer Wireless-N 1103 Network Adapter 
IPv4 Address. . . . . . . . . . . : 192.168.1.2(Preferred) 
Description . . . . . . . . . . . : Atheros AR8151 PCI-E Gigabit Ethernet Controller (NDIS 6.20) 
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1 
IPv4 Address. . . . . . . . . . . : 192.168.122.1(Preferred) 
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8 
IPv4 Address. . . . . . . . . . . : 192.168.88.1(Preferred) 
Description . . . . . . . . . . . : Microsoft ISATAP Adapter 
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2 
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3 
Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface 
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #4 
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #5 

我要的是:

Description . . . . . . . . . . . : Killer Wireless-N 1103 Network Adapter 
IPv4 Address. . . . . . . . . . . : 192.168.1.2(Preferred) 
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1 
IPv4 Address. . . . . . . . . . . : 192.168.122.1(Preferred) 
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8 
IPv4 Address. . . . . . . . . . . : 192.168.88.1(Preferred) 

回答

1

替代解决方案:

[regex]$regex = '(?ms)^\s*(Description[^\r]+\r\n\s*IPv4[^\r]+)\r' 
$regex.matches(((ipconfig /all) -match '^\s*Description|IPv4') -join "`r`n") | 
foreach {$_.groups[1].value -replace '\. ',''} 
2

如果要提取所有说明对IPv4启用适配器,你可以尝试这样的事:

ipconfig /all | Select-String "IPv4" -AllMatches -SimpleMatch -Context 5 | % { 
    $_.Context.Precontext -match "Description" -replace 'Description(?:[^:]+):(.*)$', '$1' 
} 
    Intel(R) 82579V Gigabit Network Connection 

要与您的代码得到它,试试这个:

ipconfig /all | where-object { 
    $_ -match "IPv4" -or $_ -match "Description" 
} | Select-String "IPv4" -SimpleMatch -AllMatches -Context 1 | % { 
    $_.context.precontext -replace 'Description(?:[^:]+):(.*)$', '$1' 
} 

编辑对不起,我错误地看你的问题。我以为你只想描述。这说明对于IPv4活动适配器的描述和IP线路

ipconfig /all | Select-String "IPv4" -AllMatches -SimpleMatch -Context 5 | % { 
    $_.Context.Precontext -match "Description" 
    $_.Line 
} 

Description . . . . . . . . . . . : Intel(R) 82579V Gigabit Network Connection 
IPv4 Address. . . . . . . . . . . : xx.xx.xx.xx(Preferred) 
+0

这将显示没有可能有用的IP的活动适配器。 +1 – 2013-02-21 19:47:39

+0

我增加了另一个解决方案。起初我一定误解了你的问题。不过,现在你有了一些选择+如何用正则表达式提取描述的例子(IP是通过用“IP”替换“描述”和去除正则表达式中的美元符号来完成的):) – 2013-02-21 20:03:52

+0

这是一个很好的选择,使用不同的方法,我可以选择PS作为'我的'工具箱中的相关工具。 – 2013-02-22 04:13:37

1

另一种选择,它只是跟踪输出找到的最后一个说明:

switch -regex (ipconfig /all) { 'IPv4' { $d + $_ } 'Description' { $d = @($_) } } 


此外,-match comparison operator可以在一个数组和单个字符串上工作。因此使用(ipconfig /all) -match 'IPv4|Description'相当于原始的ipconfig /all | where { $_ -match 'IPv4' -or $_ -match 'Description' },它会单独检查每条线。

+0

+1这确实提供了我在原始帖子中要求的内容。这是另一种有助于使PS与我相关的方法。我根本没有在PS中使用开关。 – 2013-03-04 16:38:21