如何在Windows中获取有关其他(与perl.exe无关)进程的信息?
说,我有一个脚本想要监控有关所有firefox.exe
进程的一些基本信息。
我想做点什么:
#!perl -w
use warnings;
use strict;
use MagicModule qw/ read_process_info /;
use Data::Dumper;
my $ps = MagicModule::read_process_info();
print Dumper($ps);
并看到类似的内容:
$VAR = [
{
ProcessName => 'firefox.exe',
PID => 1234,
PirvateBytes => 21153546,
WorkingSet => 134566354,
# etc...
},
{
ProcessName => 'firefox.exe',
PID => 1556,
PirvateBytes => 15435487,
WorkingSet => 245774448,
# etc...
},
{
ProcessName => 'calc.exe',
PID => 2777,
PirvateBytes => 1024,
WorkingSet => 4096,
# etc...
},
];
因为我通过解析Systinternals的PsExec的输出来获取此信息,但显然这根本没有效率。
获取此数据的Perl方法有哪些?
(在Windows XP-Windows 7上使用Strawberry Perl的解决方案将是首选。)
答案 0 :(得分:5)
使用Win32::OLE
的WMI是我见过的最简单的方法之一:
use Win32::OLE qw<in>;
use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;
my $objWMIService
= Win32::OLE->GetObject( 'winmgmts:\\\\localhost\root\CIMV2' )
or die 'WMI connection failed.\n'
;
my @processes
= in $objWMIService->ExecQuery(
'SELECT * FROM Win32_Process'
, 'WQL'
, wbemFlagReturnImmediately | wbemFlagForwardOnly
);
然后你可以参考对象定义here。请记住,每个字段都可以像哈希键或方法名称一样访问:($_->{Caption}
或$_->Caption
)