in reply to Re: Want to Start / Stop windows service through executing Perl Script
in thread Want to Start / Stop windows service through executing Perl Script

Thnx Martin for replying. Can you please provide me a small sample script?
  • Comment on Re^2: Want to Start / Stop windows service through executing Perl Script

Replies are listed 'Best First'.
Re^3: Want to Start / Stop windows service through executing Perl Script
by Corion (Patriarch) on Jun 01, 2009 at 07:12 UTC

    What code have you written and how does it fail for you?

    Perlmonks is not a code writing service, so please show us what efforts you've made and where you encountered problems.

      I know it very well Corion. But i guess its for helping atleast. Well anyway, Me with my friend were able to write script upto checking up service status using below code:
      #Script to list all of the currently running services using WMI query use Win32::OLE; use Win32::OLE 'in'; use Win32::OLE::Const; use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; my @servicestate = ("Stopped","Started"); my $computer = "localhost"; # can be set to point to other computers. +.. my $wmiObj = Win32::OLE->GetObject("winmgmts:\\\\$computer\\root\\CIMV +2") or die "WMI connection failed.\n"; #List all services... #my $servSet = $wmiObj->ExecQuery("SELECT * FROM Win32_Service", "WQL" +, # wbemFlagReturnImmediately | wbemFlagForwardOnly); #Get only running services my $servSet = $wmiObj->ExecQuery("SELECT * FROM Win32_Service WHERE st +arted = 1", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly); foreach my $serv (in $servSet) { my $sname = $serv->{name}; my $sstate = $serv->{started}; my $ssmode = $serv->{startmode}; print "Service: " . $sname . " - " . $servicestate[$sstate] . " - " . $ssmode . "\n"; }

        How does this code relate to the problem at hand?

        But as you're already using WMI, you can just use the service objects to start/stop the service. Just call ->StartService or ->StopService on the service respectively.