I need to do this myself from time to time so here you go:

#!/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}; } }

cheers

tachyon


In reply to Re: Killing processes on remote win32 machines by tachyon
in thread Killing processes on remote win32 machines by martymart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.