Question

I’m looking for the most standard way to get a list of running processes (and services) on a Windows machine. It’s important not to use « modern » stuff because I’ll deploy that program on old servers.

Any idea?

Was it helpful?

Solution

As skp mentioned, the tasklist command can do it (tested on Windows XP).

Here is a small script that creates a hash of processes by PID:

use warnings;
use strict;

my @procs = `tasklist`;

#Find position of PID based on the ===== ====== line in the header
my $pid_pos;
if ($procs[2] =~ /^=+/)
{
    $pid_pos = $+[0]+1;
}
else
{
    die "Unexpected format!";   
}

my %pids;
for (@procs[3 .. $#procs])
{
    #Get process name and strip whitespace
    my $name = substr $_,0,$pid_pos;
    $name =~s/^\s+|\s+$//g;

    #Get PID
    if (substr($_,$pid_pos) =~ /^\s*(\d+)/)
    {
        $pids{$1} = $name;  
    }
}

use Data::Dumper;
print Dumper %pids;

Another approach that might be useful is Win32::Process::List. It gets the process list using core Windows C functions. It appears to work with old versions of Perl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top