PID of Service

PID of Service

am 19.12.2007 15:07:00 von Ferry Bolhar

Good day to all,

Given the name of a Win32 service, is there a way to find out its PID from a
Perl script?

I looked at Win32::Service and Win32::Daemon modules, but couldn't find a
way for this.

Kind greetings,

--
Ing Ferry Bolhar
Magistrat der Stadt Wien - MA 14
A-1010 Wien
E-Mail: ferdinand.bolhar-nordenkampf@wien.gv.at

Re: PID of Service

am 19.12.2007 16:07:31 von Christian Winter

Ferry Bolhar wrote:
> Good day to all,
>
> Given the name of a Win32 service, is there a way to find out its PID from a
> Perl script?
>
> I looked at Win32::Service and Win32::Daemon modules, but couldn't find a
> way for this.

You can gather that info from the WMI (quick'n'dirty):
------------------------------------------------------------ ---------
#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use Win32::OLE qw(in);

my $dbh = DBI->connect('dbi:WMI:');

my $sth = $dbh->prepare(< SELECT * FROM Win32_Service WHERE Name = '$ARGV[0]'
WQL

$sth->execute();
if (my $row = $sth->fetchrow()) {
printf( "%-20s - %10s %8i\n\t%s\n",
$row->Name, $row->State, $row->ProcessID, $row->PathName
);
}
------------------------------------------------------------ -----------

Greetings
-Chris

Re: PID of Service

am 19.12.2007 19:41:33 von Ferry Bolhar

Christian Winter:

> You can gather that info from the WMI (quick'n'dirty):

[...]

Looks great! Does this work from other hosts as well?

Many thanks,

--
Ing Ferry Bolhar
Magistrat der Stadt Wien - MA 14
A-1010 Wien
E-Mail: ferdinand.bolhar-nordenkampf@wien.gv.at

Re: PID of Service

am 19.12.2007 22:33:08 von Ron Bergin

On Dec 19, 6:07 am, "Ferry Bolhar" wrote:
> Good day to all,
>
> Given the name of a Win32 service, is there a way to find out its PID from a
> Perl script?
>
> I looked at Win32::Service and Win32::Daemon modules, but couldn't find a
> way for this.
>
> Kind greetings,
>
> --
> Ing Ferry Bolhar
> Magistrat der Stadt Wien - MA 14
> A-1010 Wien
> E-Mail: ferdinand.bolhar-nordenka...@wien.gv.at

use strict;
use warnings;
use Win32::Process::Info;

my $pi = Win32::Process::Info->new ();
my ($info) = grep {$_->{Name} =~ m/msnmsgr/} $pi->GetProcInfo();
print $info->{ProcessId};

Re: PID of Service

am 20.12.2007 09:44:58 von Christian Winter

Ferry Bolhar schrieb:
> Christian Winter:
>
>> You can gather that info from the WMI (quick'n'dirty):
>
> [...]
>
> Looks great! Does this work from other hosts as well?

Certainly, though with the docs for DBD::WMI alternating between
lacking and plain wrong, it's not that easy to guess:

my $dbh = DBI->connect('dbi:WMI:' . $hostname);

will do the trick.

-Chris