BernieC has asked for the wisdom of the Perl Monks concerning the following question:

Given its running-name, like "Virtual Disk Service", is there some sort-of simple way to find its PID? I found a code snippet on stackoverflow and I can't understand it at all:
use Win32::OLE qw(in); sub matching_processes { my($pattern) = @_; my $objWMI = Win32::OLE->GetObject('winmgmts://./root/cimv2'); my $procs = $objWMI->InstancesOf('Win32_Process'); my @hits; foreach my $p (in $procs) { push @hits => [ $p->Name, $p->ProcessID ] if $p->Name =~ /$pattern/; }
I've looked at Win32::OLE and I cannot figure out what the "GetObject" does. Its description is
Win32::OLE->GetObject(MONIKER, DESTRUCTOR) The GetObject() class method returns an OLE reference to the specified object. The object is specified by a pathname optionally followed by additional item subcomponent separated by exclamation marks '!'. The optional DESTRUCTOR argument has the same semantics as the DESTRUCTOR in new() or GetActiveObject()
Which I mostly can't understand. And worse, the man page for ::OLE doesn't seem to have a InstancesOf method {nor did I see anything in the man page about an "in" that could be exported}. I'm completely baffled. There must be a less obscure want to do this.

Replies are listed 'Best First'.
Re: Find a windows process
by Discipulus (Canon) on May 16, 2022 at 16:50 UTC
      I didn't see the Win32::Process::List but you answered the question that was hidden in my inquiry -- I know that Task Manager has a "detail" tab that lists the names/processes but I couldn't see a way to {a} run that from perl, and {b} extract that info task manager is strictly window-only.. but "tasklist" -- You've solved my problem *twice*!! thanks...