Try something like
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE qw( in );
use Win32::OLE::Variant;
use Win32::Process;
my $CLASS = "winmgmts:{impersonationLevel=impersonate}\\\\$ENV{COMPUTE
+RNAME}\\Root\\cimv2";
my $WMI = Win32::OLE->GetObject( $CLASS ) || die "Cannot Get list of P
+IDS\n";
foreach my $Proc ( sort {lc $a->{Name} cmp lc $b->{Name}} in( $WMI->In
+stancesOf( "Win32_Process" ) ) )
{
if ($Proc->{Name} eq "Whatever you are looking for.exe")
{
print "$Proc->{ProcessID} is STILL RUNNING\n";
# We Dont Want to KILL the PID ---Win32::Process::KillProcess(
+$Proc->{ProcessID}, 5);
# Other Processing Here..;-)
}
}
If on the other hand you already know the PID you can simply run
use Win32::process;
Win32::Process::KillProcess($PID,5);
Keep in mind though that $PID is a perlvar and if I ma correct returns the PID of perl.exe that is running your current script. Maybe you should change your var name..
From the perlvar help - ($PID/$$) "The process number of the Perl running this script. You should consider this variable read-only, although it will be altered across fork() calls."
Code Ripped from a working script I have running... This peice has not been tested on its own but should work..;-)
Update: Thanks to BrowserUk for reminding me what the question really was..!
-----
Of all the things I've lost in my life, its my mind I miss the most.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.