返回列表 发帖

[转载代码] [PowerShell每日技巧]对命令的结果进行过滤(20140224)

Comparison operators act like filters when applied to arrays. So any console command that outputs multiple text lines can be used with comparison operators.

This example will use netstat.exe to get only established network connections, then to get only established network connections to any server that has "stor" in its name, and then uses ipconfig to get the current IPv4 address:

PS C:\> @(netstat) -like '*establ*'
  TCP    192.168.0.10:49863   BatHome01:1080        ESTABLISHED
  TCP    192.168.0.10:49864   BatHome02:1080        ESTABLISHED
  TCP    192.168.0.10:49866   HomeBat01:1080        ESTABLISHED
  TCP    192.168.0.10:49898   HomeBat02:1080        ESTABLISHED


The trick is to enclose the console command in @() which makes sure that the result is always an array.

http://powershell.com/cs/blogs/tips/archive/2014/02/24/filtering-text-based-command-output.aspx

可以用 Select-String 来代替 -like
  1. netstat | Select-String "establ"
复制代码

TOP

返回列表