Win32::OLE, WMI and executing methods

Discussion in 'Perl Misc' started by Daniel Berger, Jun 3, 2004.

  1. 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
     
    Daniel Berger, Jun 3, 2004
    #1
    1. Advertising

  2. Daniel Berger

    Petri Guest

    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/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
     
    Petri, Jun 3, 2004
    #2
    1. Advertising

  3. Daniel Berger

    Ben Morrow Guest

    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/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

    --
    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)
     
    Ben Morrow, Jun 3, 2004
    #3
  4. 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/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
     
    Daniel Berger, Jun 4, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean
    Replies:
    1
    Views:
    1,257
    jose maria
    Jul 10, 2003
  2. woof
    Replies:
    2
    Views:
    99
  3. Lance Hoffmeyer
    Replies:
    0
    Views:
    263
    Lance Hoffmeyer
    Nov 17, 2003
  4. Daniel Berger

    OLE, WMI and ExecMethod - possible?

    Daniel Berger, Jun 15, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    285
    Petri
    Jun 15, 2004
  5. HmJ
    Replies:
    0
    Views:
    253
Loading...

Share This Page