@@ -10,7 +10,7 @@ import * as path from 'path';
1010import * as vscode from 'vscode' ;
1111
1212import { PlatformInformation } from '../platform' ;
13- import { getExtensionPath } from '../common' ;
13+ import { findPowerShell , getExtensionPath } from '../common' ;
1414
1515export interface AttachItem extends vscode . QuickPickItem {
1616 id : string ;
@@ -265,7 +265,7 @@ export class RemoteAttachPicker {
265265 }
266266}
267267
268- class Process {
268+ export class Process {
269269 constructor ( public name : string , public pid : string , public commandLine : string , public flags : number ) { }
270270
271271 public toAttachItem ( ) : AttachItem {
@@ -282,7 +282,8 @@ class Process {
282282export class DotNetAttachItemsProviderFactory {
283283 static Get ( ) : AttachItemsProvider {
284284 if ( os . platform ( ) === 'win32' ) {
285- return new WmicAttachItemsProvider ( ) ;
285+ const pwsh : string | undefined = findPowerShell ( ) ;
286+ return pwsh ? new CimAttachItemsProvider ( pwsh ) : new WmicAttachItemsProvider ( ) ;
286287 }
287288 else {
288289 return new PsAttachItemsProvider ( ) ;
@@ -423,6 +424,43 @@ export class PsOutputParser {
423424 }
424425}
425426
427+ export class CimAttachItemsProvider extends DotNetAttachItemsProvider {
428+ constructor ( private pwsh : string ) { super ( ) ; }
429+
430+ protected async getInternalProcessEntries ( ) : Promise < Process [ ] > {
431+ const pwshCommand : string = `${ this . pwsh } -NoProfile -Command` ;
432+ const cimCommand : string = 'Get-CimInstance Win32_Process | Select-Object Name,ProcessId,CommandLine | ConvertTo-JSON' ;
433+ const processes : string = await execChildProcess ( `${ pwshCommand } "${ cimCommand } "` , undefined ) ;
434+ return CimProcessParser . ParseProcessFromCim ( processes ) ;
435+ }
436+ }
437+
438+ type CimProcessInfo = {
439+ Name : string ;
440+ ProcessId : number ;
441+ CommandLine : string | null ;
442+ } ;
443+
444+ export class CimProcessParser {
445+ private static get extendedLengthPathPrefix ( ) : string { return '\\\\?\\' ; }
446+ private static get ntObjectManagerPathPrefix ( ) : string { return '\\??\\' ; }
447+
448+ // Only public for tests.
449+ public static ParseProcessFromCim ( processes : string ) : Process [ ] {
450+ const processInfos : CimProcessInfo [ ] = JSON . parse ( processes ) ;
451+ return processInfos . map ( info => {
452+ let cmdline : string | undefined = info . CommandLine || undefined ;
453+ if ( cmdline ?. startsWith ( this . extendedLengthPathPrefix ) ) {
454+ cmdline = cmdline . slice ( this . extendedLengthPathPrefix . length ) ;
455+ }
456+ if ( cmdline ?. startsWith ( this . ntObjectManagerPathPrefix ) ) {
457+ cmdline = cmdline . slice ( this . ntObjectManagerPathPrefix . length ) ;
458+ }
459+ return new Process ( info . Name , `${ info . ProcessId } ` , cmdline , null ) ;
460+ } ) ;
461+ }
462+ }
463+
426464export class WmicAttachItemsProvider extends DotNetAttachItemsProvider {
427465 protected async getInternalProcessEntries ( ) : Promise < Process [ ] > {
428466 const wmicCommand = 'wmic process get Name,ProcessId,CommandLine /FORMAT:list' ;
0 commit comments