#!/usr/bin/perl -w
use strict;
use Win32::OLE;
my $server = 'my-server'; # Note '.' is localhost;
my $username = 'administrator';
my $password = 'isverysecret';
my $c = connectServer( $server, $username, $password );
show_running_service($c);
show_running_process($c);
#killProcess( $c, 2052 );
sub connectServer {
my ( $server, $username, $password ) = @_;
$server ||= '.'; # localhost
my $locator = Win32::OLE->new("WbemScripting.SWbemLocator")
or die "Can't access WMI on local machine.", Win32::OLE->LastE
+rror;
my $serverConn = $locator->ConnectServer($server, "root/cimv2", $u
+sername, $password)
or die "Can't access WMI on remote machine $server: ", Win32::
+OLE->LastError;
return $serverConn;
}
sub killProcess {
my ( $serverConn, $pid ) = @_;
my $obj = $serverConn->Get( "Win32_Process.Handle=$pid" )
or die "Could not get an object handle for $pid: ", Win32::OLE
+->LastError;
$obj->Terminate(); # returns 0 on success and 0 on failure!
}
sub show_running_service {
my $serverConn = shift;
my $serviceSet = $serverConn->ExecQuery('SELECT * FROM Win32_Servi
+ce WHERE State="Running"')
or die "Can't get process list from server: ", Win32::OLE->Las
+tError;
for my $service ( in $serviceSet ) {
printf "%s\n\tPID: %-6d Start Mode: %s\n\n",
$service->{Description}, $service->{ProcessId},$service->{
+StartMode};
}
}
sub show_running_process {
my $serverConn = shift;
my $processSet = $serverConn->ExecQuery('SELECT * FROM Win32_Proce
+ss')
or die "Can't get process list from server: ", Win32::OLE->Las
+tError;
for my $process ( in $processSet ) {
printf "%-6d %s\n", $process->{ProcessId}, $process->{Descrip
+tion};
}
}
|