In article < >, Daniel Berger
says...
> 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/de...wmi_method.asp
# Return values from StopService():
#
http://msdn.microsoft.com/library/de...32_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