你的分享就是我们的动力 ---﹥

Perl 操作 Windows 的系统服务

时间:2013-05-30 00:05来源:www.chengxuyuans.com 点击:

代码简介

暂无

Perl代码片段

#!/usr/bin/perl -w

use Win32::Service;


$host = '';
$status = Win32::Service::GetServices($host, \%services);

if ($status) {
   print_hash( \%services );
} else {
   print_error();
}


sub print_hash {
   my($hash_ref) = $_[0];
   @keys = keys( %$hash_ref );
   @sorted = sort( @keys );
   foreach $key (@sorted) {
      print "$key $$hash_ref{$key}\n";
   }
}

sub print_error {
    print Win32::FormatMessage( Win32::GetLastError() );
}

Perl代码片段

$status = Win32::Service::StartService($host, $service);

#!/usr/bin/perl -w

use Win32::Service;

$num_args = scalar( @ARGV );

$host = '';

$service = "yourServiceName";

$status = Win32::Service::StartService($host, $service);

if ($status) {
print "Started $service.\n";
} else {
    print_error();
}   
sub print_error {
    print Win32::FormatMessage( Win32::GetLastError() );
}

Perl代码片段

#  $status = Win32::Service::StopService($host, $service);

#!/usr/bin/perl -w

use Win32::Service;
$host = 'yourhost';
$service = $ARGV[0];

print "Starting $service on $host.\n";

$status = Win32::Service::StopService($host, $service);

if ($status) {
   print "Started $service.\n";
} else {
   print_error();
}

sub print_error {
    print Win32::FormatMessage( Win32::GetLastError() );
}

转载注明地址:http://www.chengxuyuans.com/Perl/63093.html