Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Killing processes on remote win32 machines

by martymart (Deacon)
on Mar 11, 2004 at 13:26 UTC ( [id://335802]=perlquestion: print w/replies, xml ) Need Help??

martymart has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks

I am trying to find a way to kill processes on remote win32 machines. I have used system calls to pskill successfully (very useful app available at http://www.sysinternals.com/ntw2k/freeware/pskill.shtml
But what I'm really looking for is a perl only solution to achieve the same thing. After some searching, Win32::IProc seemed to be the way to go, but that module no longer seems to be supported or available. If any monk could point me to the a module that would achieve this (or even some sample code that does the same), I would be eternally grateful.

Martymart

  • Comment on Killing processes on remote win32 machines

Replies are listed 'Best First'.
Re: Killing processes on remote win32 machines
by tachyon (Chancellor) on Mar 11, 2004 at 15:53 UTC

    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

Re: Killing processes on remote win32 machines
by tachyon (Chancellor) on Mar 11, 2004 at 13:55 UTC

    You can find kill.vbs from the windows NT reskit here if you don't have the reskit. Either convert that to Perl using OLE or just call it with a system call and the args it wants.

    kill.vbs /X <processid> [/S <server>] [/U <username>] [/W <password>] + [/O <outfile>]

    cheers

    tachyon

Re: Killing processes on remote win32 machines
by maa (Pilgrim) on Mar 11, 2004 at 13:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://335802]
Approved by AcidHawk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found