Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Win32::OLE, WMI and executing methods

Reply
Thread Tools

Win32::OLE, WMI and executing methods

 
 
Daniel Berger
Guest
Posts: n/a
 
      06-03-2004
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
 
Reply With Quote
 
 
 
 
Petri
Guest
Posts: n/a
 
      06-03-2004
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

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      06-03-2004

Quoth Petri <>:
> 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__


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

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
(Kate Rusby)
 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      06-03-2004
Petri <> wrote in message news:<>...
> 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


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
executing list of methods (and collecting results) Gerardo Herzig Python 5 09-21-2007 09:05 AM
Executing multiple methods on same java instance lourduraj.s@gmail.com Java 5 05-06-2007 12:14 AM
Programatically downloading javascript files and executing methods from it... Raj Javascript 1 05-02-2005 12:31 PM
Executing a remote process via WMI in Win32. Sean Python 1 07-10-2003 12:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57