Win32::OLE, WMI and executing methods

D

Daniel Berger

Hi all,

A.S. Perl 5.8.3
Windows XP Pro

I'm familiar with how to get all InstancesOf() a WMI class (the only
examples I could find), but I'm confused as to how to call methods on
a class. For example, I know how to iterate over a list of Services,
but how do I call the StopService() method using Win32::OLE and WMI?

Thanks.

Dan
 
P

Petri

Daniel Berger said:
A.S. Perl 5.8.3
Windows XP Pro
I'm familiar with how to get all InstancesOf() a WMI class
(the only examples I could find), but I'm confused as to how to
call methods on a class. For example, I know how to iterate over
a list of Services, but how do I call the StopService() method
using Win32::OLE and WMI?

This short example seems to work fine:
#!/usr/bin/perl
# How to call a method:
#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/calling_a_wmi_method.asp
# Return values from StopService():
#
http://msdn.microsoft.com/library/d...stopservice_method_in_class_win32_service.asp

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

my $wmi = Win32::OLE->GetObject("winmgmts:");

my $serv_set = $wmi->InstancesOf("Win32_Service");
my $serv;
foreach $serv (in($serv_set)) {
next unless ($serv->{'Name'} eq 'SETI'); # Find SETI service.
if ($serv->{'State'} eq 'Running') { # If it's running, stop it.
my $ok = $serv->StopService();
print $serv->{'Name'} . ' has been succesfully stopped.' if (!$ok);
} else { # If service is already stopped or in some other mysterious
# state, quit loop.
last;
}
}
__END__


Just follow the commented URLs for more info.
I leave error management as an exercise to the reader. :)

Hope this helps!


Petri
 
B

Ben Morrow

Quoth Petri said:
This short example seems to work fine:
#!/usr/bin/perl
# How to call a method:
#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/calling_a_wmi_method.asp
# Return values from StopService():
#
http://msdn.microsoft.com/library/d...stopservice_method_in_class_win32_service.asp

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

my $wmi = Win32::OLE->GetObject("winmgmts:");

my $serv_set = $wmi->InstancesOf("Win32_Service");
my $serv;
foreach $serv (in($serv_set)) {
next unless ($serv->{'Name'} eq 'SETI'); # Find SETI service.
if ($serv->{'State'} eq 'Running') { # If it's running, stop it.
my $ok = $serv->StopService();
print $serv->{'Name'} . ' has been succesfully stopped.' if (!$ok);
} else { # If service is already stopped or in some other mysterious
# state, quit loop.
last;
}
}
__END__

A bit more Perlish would be

my @srvs = grep { $_->{Name} eq 'SETI' } in $srv_set;
@srvs or die "SETI service not installed"
@srvs == 1 or die "More than one SETI service installed";
my $srv = $srvs[0];

if ($srv->{State} eq 'Running') {
my $err = $srv->StopService;
$err and die "StopService failed for $srv->{Name}: $err";
print "$srv->{Name} has been successfully stopped";
}

Ben
 
D

Daniel Berger

Petri said:
This short example seems to work fine:
#!/usr/bin/perl
# How to call a method:
#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/calling_a_wmi_method.asp
# Return values from StopService():
#
http://msdn.microsoft.com/library/d...stopservice_method_in_class_win32_service.asp

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

my $wmi = Win32::OLE->GetObject("winmgmts:");

my $serv_set = $wmi->InstancesOf("Win32_Service");
my $serv;
foreach $serv (in($serv_set)) {
next unless ($serv->{'Name'} eq 'SETI'); # Find SETI service.
if ($serv->{'State'} eq 'Running') { # If it's running, stop it.
my $ok = $serv->StopService();
print $serv->{'Name'} . ' has been succesfully stopped.' if (!$ok);
} else { # If service is already stopped or in some other mysterious
# state, quit loop.
last;
}
}
__END__


Just follow the commented URLs for more info.
I leave error management as an exercise to the reader. :)

Hope this helps!


Petri

Ah, thanks. I was trying to call it as a class method based on
earlier docs I had read:
$wmi->ExecMethod("Win32_Service","StopService","SETI"), but it didn't
work, although it seems that ought to be possible.

Thanks again.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top