http://www.perlmonks.org?node_id=155959

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

How do I kill a process on a remote win32 machine?

Originally posted as a Categorized Question.

  • Comment on How do i kill a process on a remote win32 machine

Replies are listed 'Best First'.
Re: How do i kill a process on a remote win32 machine
by Util (Priest) on Apr 03, 2002 at 02:08 UTC

    Use WMI (Windows Management Instrumentation).

    This book has 2 pages on WMI details, and 4 sample programs:
    Win32 Perl Scripting: The Administrator's Handbook

    This book has 7 pages on WMI details, and 4 code snippets:
    Perl for System Administration: Managing multi-platform environments with Perl

    A WMI tutorial, SDK, and documentation are available for download from Microsoft.

    Working skeleton code, derived (and bug-fixed) from both books:

    #!/usr/bin/perl -W # WMI_for_monks.pl use warnings 'all'; use strict; use Win32::OLE('in'); my ($Machine, $pid_to_kill) = @ARGV; $Machine =~ s{^[\\/]{2,}}{}; my $CLASS = 'WinMgmts:{impersonationLevel=impersonate}!//' . $Machine; my $WMI = Win32::OLE->GetObject( $CLASS ) or die Win32::OLE->LastError(); foreach my $proc (in $WMI->InstancesOf('Win32_Process')){ next unless $proc->{ProcessId} == $pid_to_kill; printf "Killing:%7d\t%s\n", $proc->{ProcessId}, $proc->{Name}; $proc->Terminate(0); }

    This command:
    WMI_for_monks.pl COSMOS 952
    produced this output:
    Killing:    952 winbff32.exe

Re: How do i kill a process on a remote win32 machine
by Marza (Vicar) on Apr 02, 2002 at 20:06 UTC

    Use the Win32::Service

    You have a function

    StopService(hostName, serviceName) http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/site/lib/Win32/Service.html
Re: How do i kill a process on a remote win32 machine
by Marza (Vicar) on Apr 03, 2002 at 01:30 UTC

    Strange by my reply disappeared.

    Here it is again...

    Sorry, usually when I get a process question, they mean service....

    Take a look at Win32::Process

    You will have a function

    Win32::Process::KillProcess($pid, $exitcode)

    Terminates any process identified by $pid. $exitcode will be set to the exit code of the process.

    http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/site/lib/Win32/Process.html

      Ignore this! A case of posting without a license!

      This is only works locally!

      Thanks to Util for catching this!

Re: How do i kill a process on a remote win32 machine
by Marza (Vicar) on Apr 02, 2002 at 20:10 UTC

    Assuming you mean a service.

    Use the Win32::Service

    You have a function

    StopService(hostName, serviceName)

    http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/site/lib/Win32/Service.html

    Originally posted as a Categorized Answer.

Re: How do i kill a process on a remote win32 machine
by cab (Beadle) on Apr 02, 2002 at 20:22 UTC
    Actually I meant a program or a process. Stopping a service would not have been a problem. I can find the right pid, but how to kill it on a remote machine using only perl and no external software.

    Thanks

    /cab

      Sorry, usually when I get a process question, they mean service....

      Take a look at this mod.

      You will have a function

      Win32::Process::KillProcess($pid, $exitcode)

      Terminates any process identified by $pid. $exitcode will be set to the exit code of the process.

      http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/site/lib/Win32/Process.html

Re: How do i kill a process on a remote win32 machine
by Anonymous Monk on Aug 19, 2003 at 07:09 UTC
    I am trying findout whether a process is already running in Windows 2000 Professional using a perl script that is given below. Sometimes I am getting the following error:
    Win32::OLE(0.1603) error 0x800401e4: "Invalid syntax" after character 0 in "WinMgmts:{impersonationLevel=impersonate}!// +"
    Any idea why it is giving this sometimes not always.
    sub checkProcessExists { my $newPid = $$; // Get Process id my $file; //old Process id is stored in this file my ($flag, $oldPid); unless (open (PRID, $file)) { $flag = 0; } else { $oldPid = <PRID>; chomp ($oldPid); close (PRID); $flag = 1; } if ($flag) { my $CLASS = 'WinMgmts:{impersonationLevel=impersonate}!//'; my $WMI = Win32::OLE->GetObject( $CLASS ) or print ("OLE, GetO +bject error: " . Win32::OLE->LastError()); my $pid; foreach my $proc (in $WMI->InstancesOf('Win32_Process')){ $pid = $proc->{ProcessId}; if ($pid == $oldPid) { $flag = 1; last; } else { $flag = 0; } } } if (!$flag) { unless (open (PRID, ">$file")) { print ("Unable to open $file"); exit(); } else { print PRID $newPid; close (PRID); } } else { print ("$name (PID: $oldPid) is still running. Try again later +"); } return $flag; }

    Originally posted as a Categorized Answer.

Re: How do i kill a process on a remote win32 machine
by cab (Beadle) on Apr 03, 2002 at 06:48 UTC
    Thanks Marza for your input. Actually i got this working ok on my localhost. The problem is that i need to do this on a server without installing perl to that server, so i need to do this over LAN.
    I couldn't find a way to do this with Win32::Process::KillProcess.

    /cab

    Originally posted as a Categorized Answer.

Re: How do i kill a process on a remote win32 machine
by cab (Beadle) on Apr 03, 2002 at 06:52 UTC
    ++ Util

    I need to try your suggestion.

    /cab

    Originally posted as a Categorized Answer.