1- const { execSync } = require ( 'child_process' ) ;
1+ const { execSync, execFileSync } = require ( 'child_process' ) ;
22const path = require ( 'path' ) ;
33const os = require ( 'os' ) ;
44const fs = require ( 'fs' ) ;
@@ -18,18 +18,19 @@ const IS_WINDOWS = process.platform === 'win32';
1818function getProcessList ( ) {
1919 try {
2020 if ( IS_WINDOWS ) {
21- // wmic provides CSV-formatted process data
22- const output = execSync ( 'wmic process get CommandLine,ProcessId /format:csv' , {
21+ // Use PowerShell Get-Process (WMIC is deprecated in Windows 10/11)
22+ const output = execFileSync ( 'powershell' , [ '-Command' , 'Get-Process | Select-Object Id, Path, CommandLine | ConvertTo-Csv -NoTypeInformation' ] , {
2323 encoding : 'utf-8' ,
2424 maxBuffer : 10 * 1024 * 1024 ,
2525 } ) ;
26- // Parse CSV: skip header, split by comma
26+ // Parse CSV: skip header
2727 const lines = output . split ( '\n' ) . slice ( 1 ) ;
2828 return lines . map ( line => {
2929 const parts = line . split ( ',' ) ;
30- if ( parts . length < 2 ) return null ;
31- const commandLine = parts . slice ( 0 , - 1 ) . join ( ',' ) . trim ( ) . replace ( / ^ " | " $ / g, '' ) ;
32- const pid = parts [ parts . length - 1 ] . trim ( ) ;
30+ if ( parts . length < 3 ) return null ;
31+ const pid = parts [ 0 ] . trim ( ) . replace ( / ^ " | " $ / g, '' ) ;
32+ const commandLine = parts [ 2 ] . trim ( ) . replace ( / ^ " | " $ / g, '' ) ;
33+ if ( ! pid || ! commandLine ) return null ;
3334 return { commandLine, pid } ;
3435 } ) . filter ( Boolean ) ;
3536 } else {
@@ -49,8 +50,8 @@ function getProcessList() {
4950function getListeningPorts ( pid ) {
5051 try {
5152 if ( IS_WINDOWS ) {
52- // netstat -ano shows PID in the last column
53- const output = execSync ( `netstat -ano | findstr ${ pid } ` , {
53+ // Use PowerShell to get netstat output and filter by PID
54+ const output = execFileSync ( 'powershell' , [ '-Command' , `netstat -ano | Select-String " ${ pid } $"` ] , {
5455 encoding : 'utf-8' ,
5556 maxBuffer : 10 * 1024 * 1024 ,
5657 } ) ;
0 commit comments