Execute Windows command prompt output to get PID

I'm going to collect just the PID value outputted when running this netstat command:

netstat -a -o -n -p tcp | findstr -i "CLOSE_WAIT"

      

I intend to use the PID and create a script that will run taskkill /PID pidfoundhere

to remove any sockets with CLOSE_WAIT state.

+3


source to share


1 answer


@echo off
    for /f "tokens=5" %%a in ('
        netstat -noa -p tcp ^| find /i "CLOSE_WAIT"
    ') do if not "%%a"=="0" echo taskkill /pid %%a

      

Use the command for

to split the line using spaces as delimiters, get the 5th token in the line, and if there is a PID, kill the process



Commands are taskkill

displayed only on the console. If the output is correct, remove the commandecho

+4


source