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

  • Comment on How to get PID of a process given its complete command line in windows

Replies are listed 'Best First'.
Re: How to get PID of a process given its complete command line in windows
by Corion (Patriarch) on Nov 07, 2014 at 06:57 UTC

    The easiest way is to do that using WMI. A bit of Googling tells me that you can get the command line of any process using WMIC:

    WMIC PROCESS get Caption,Commandline,Processid

    Then, you just have to go through all lines in your script and you have the process id.

    See perlop for how to get the output of another process.

    Maybe you want to use DBD::WMI to avoid running an external program. Most likely, the SQL would be

    SELECT Caption , CommandLine , ProcessId FROM Win32_PROCESS
Re: How to get PID of a process given its complete command line in windows
by blindluke (Hermit) on Nov 07, 2014 at 06:55 UTC

    Take a look at the Win32::Process::List module, it's probably all you need. Alternatively, you could get away with a simple

    my @proc_lines = `tasklist`;

    but then you would have to parse the lines for the relevant info yourself. Win32::Process::List does that already.

    UPDATE: A small example from the module documentation:

    use warnings; use strict; use Win32::Process::List; my $plist = Win32::Process::List->new(); my $pid = $plist->GetProcessPid("explorer");

    - Luke

      Windows tasklist command only has the image name of the process, not its complete commandline. There can many process with same image name. Also, the Win32::Process::List module has image name not the commandline of the process.

Re: How to get PID of a process given its complete command line in windows
by boftx (Deacon) on Nov 08, 2014 at 00:00 UTC

    I'm not even certain one can begin to respond to this since a) there is no context given and b) it's technically not even a question but a statement. The answer (if it is a question) might be as simple as don't bother looking in the process list but instead look at $$.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.