LoginSignup
1
1

More than 3 years have passed since last update.

netstatでプロセス名も表示

Posted at

netstatコマンドの-oオプションで接続に関連付けられたプロセスIDが表示されますが、プロセス名を別途調べる必要があるため、プロセス名も一緒に表示するスクリプトです。

netstat_extenction.ps1
# netstat プロセス名も表示
#
# コマンドプロンプトで実行
# CMD> powershell -NoProfile -ExecutionPolicy Unrestricted .\netstat_extension.ps1
# [D] 実行しない(D)  [R] 一度だけ実行する(R)  [S] 中断(S)  [?] ヘルプ (既定値は "D"): r
#
# PowerShellで実行
# 1. 実行ポリシーを変更
#   PS> Set-ExecutionPolicy RemoteSigned⇒yes
# 2. 実行
#   PS> .\netstat_extenstion.ps1
# 3. 実行ポリシーを戻します
#   PS> Set-ExecutionPolicy Restricted⇒yes

$netstat = netstat -aon | Select-String -Pattern "(TCP|UDP)"
$ProcessList = Get-Process
foreach ($data in $netstat)
{
    $SpltArry = $data -split " "
    $PD = $spltArry[$spltarry.length - 1]
    $pn = $ProcessList | Where-Object {$_.id -eq $pd } | select processname
    $SpltArry[$SpltArry.length - 1] = $PD + " " + $PN.processname
    $SpltArry -join " "
}
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1