powershell 查看程序的tcp网络连接

在运维工作中,常常查看某个业务的网络连接状况,在这里借用netstat来实现查找连接。用hash特性避免反复。

$add=@{}
while(1){
ps|?{$_.path  -match 'E:\\games\\梦幻XX}|%{
$id=$_.id
netstat -ano |
    ForEach-Object {
       $i = $_ | Select-Object -Property Protocol , Source , Destination , Mode ,pid
       $null, $i.Protocol, $i.Source, $i.Destination, $i.Mode, $i.pid=  ($_ -split '\s{2,}')
       $i
    }|?

{$_.pid -eq $id}|%{ try{ $add+=@{$_.Destination=$_.Source} $_} catch{} }} sleep 1 } $add


posted @ 2016-03-31 16:11  zfyouxi  阅读(1370)  评论(0编辑  收藏  举报