@@ -214,7 +214,8 @@ interface ICommandLineOptions {
214214/**
215215 * Process information sourced from the system. This process info is sourced differently depending
216216 * on the operating system:
217- * - On Windows, this uses the `wmic.exe` utility.
217+ * - On Windows, this uses `powershell.exe` and a scriptlet to retrieve process information.
218+ * The wmic utility that was previously used is no longer present on the latest Windows versions.
218219 * - On Unix, this uses the `ps` utility.
219220 *
220221 * @public
@@ -281,18 +282,15 @@ export function parseProcessListOutput(
281282}
282283
283284// win32 format:
284- // Name ParentProcessId ProcessId
285- // process name 1234 5678
285+ // PPID PID NAME
286+ // 51234 56784 process name
286287// unix format:
287288// PPID PID COMMAND
288289// 51234 56784 process name
289290const NAME_GROUP : 'name' = 'name' ;
290291const PROCESS_ID_GROUP : 'pid' = 'pid' ;
291292const PARENT_PROCESS_ID_GROUP : 'ppid' = 'ppid' ;
292- const PROCESS_LIST_ENTRY_REGEX_WIN32 : RegExp = new RegExp (
293- `^(?<${ NAME_GROUP } >.+?)\\s+(?<${ PARENT_PROCESS_ID_GROUP } >\\d+)\\s+(?<${ PROCESS_ID_GROUP } >\\d+)\\s*$`
294- ) ;
295- const PROCESS_LIST_ENTRY_REGEX_UNIX : RegExp = new RegExp (
293+ const PROCESS_LIST_ENTRY_REGEX : RegExp = new RegExp (
296294 `^\\s*(?<${ PARENT_PROCESS_ID_GROUP } >\\d+)\\s+(?<${ PROCESS_ID_GROUP } >\\d+)\\s+(?<${ NAME_GROUP } >.+?)\\s*$`
297295) ;
298296
@@ -301,8 +299,7 @@ function parseProcessInfoEntry(
301299 existingProcessInfoById : Map < number , IProcessInfo > ,
302300 platform : NodeJS . Platform
303301) : void {
304- const processListEntryRegex : RegExp =
305- platform === 'win32' ? PROCESS_LIST_ENTRY_REGEX_WIN32 : PROCESS_LIST_ENTRY_REGEX_UNIX ;
302+ const processListEntryRegex : RegExp = PROCESS_LIST_ENTRY_REGEX ;
306303 const match : RegExpMatchArray | null = line . match ( processListEntryRegex ) ;
307304 if ( ! match ?. groups ) {
308305 throw new InternalError ( `Invalid process list entry: ${ line } ` ) ;
@@ -369,15 +366,20 @@ function getProcessListProcessOptions(): ICommandLineOptions {
369366 let command : string ;
370367 let args : string [ ] ;
371368 if ( OS_PLATFORM === 'win32' ) {
372- command = 'wmic.exe' ;
373- // Order of declared properties does not impact the order of the output
374- args = [ 'process' , 'get' , 'Name,ParentProcessId,ProcessId' ] ;
369+ command = 'powershell.exe' ;
370+ // Order of declared properties sets the order of the output.
371+ // Put name last to simplify parsing, since it can contain spaces.
372+ args = [
373+ '-NoProfile' ,
374+ '-Command' ,
375+ `'PPID PID Name'; Get-CimInstance Win32_Process | % { '{0} {1} {2}' -f $_.ParentProcessId, $_.ProcessId, $_.Name }`
376+ ] ;
375377 } else {
376378 command = 'ps' ;
377379 // -A: Select all processes
378380 // -w: Wide format
379381 // -o: User-defined format
380- // Order of declared properties impacts the order of the output. We will
382+ // Order of declared properties sets the order of the output. We will
381383 // need to request the "comm" property last in order to ensure that the
382384 // process names are not truncated on certain platforms
383385 args = [ '-Awo' , 'ppid,pid,comm' ] ;
@@ -654,7 +656,7 @@ export class Executable {
654656 * Get the list of processes currently running on the system, keyed by the process ID.
655657 *
656658 * @remarks The underlying implementation depends on the operating system:
657- * - On Windows, this uses the `wmic .exe` utility .
659+ * - On Windows, this uses `powershell .exe` and the `Get-CimInstance` cmdlet .
658660 * - On Unix, this uses the `ps` utility.
659661 */
660662 public static async getProcessInfoByIdAsync ( ) : Promise < Map < number , IProcessInfo > > {
@@ -693,7 +695,7 @@ export class Executable {
693695 * with the same name will be grouped.
694696 *
695697 * @remarks The underlying implementation depends on the operating system:
696- * - On Windows, this uses the `wmic .exe` utility .
698+ * - On Windows, this uses `powershell .exe` and the `Get-CimInstance` cmdlet .
697699 * - On Unix, this uses the `ps` utility.
698700 */
699701 public static async getProcessInfoByNameAsync ( ) : Promise < Map < string , IProcessInfo [ ] > > {
0 commit comments