Here is a "poor man's ps" utility written in Perl. It uses WMI, which
is standard on 2000 and XP but which you need to install on NT. WMI
is not available for 95/98/ME.
- Pat
use warnings;
use strict;
use Win32::OLE;
# Get a property, returning the empty string if it does not exist.
# (The "CommandLine" property is new with Windows XP.)
sub get_property ($$) {
my ($obj, $prop) = (@_);
# Silence warnings
my $old_warn = Win32::OLE->Option ('Warn');
Win32::OLE->Option ('Warn' => 0);
my $val = $obj->{$prop};
# Restore warnings
Win32::OLE->Option ('Warn' => $old_warn);
return (defined $val ? $val : '');
}
sub by_pid ($$) {
my ($proc1, $proc2) = @_;
return (get_property ($proc1, 'ProcessId')
<=> get_property ($proc2, 'ProcessId'));
}
# In general, bomb out completely if COM engine encounters any trouble.
Win32::OLE->Option ('Warn' => 3);
# Get a handle to the SWbemServices object of the local machine.
my $computer = Win32::OLE->GetObject ('WinMgmts:');
# Get the SWbemObjectSet of all processes.
my $processes_set = $computer->InstancesOf ('Win32_process');
# Convert set to a Perl array.
my @processes = Win32::OLE::Enum->All ($processes_set);
foreach my $process (sort by_pid @processes) {
my ($pid, $ppid, $name, $cmdline) =
(map { get_property ($process, $_) }
('ProcessId', 'ParentProcessId', 'Name', 'CommandLine'));
printf "%5s %5s %-16s %s\n", $pid, $ppid, $name, $cmdline;
}