Page 1 of 1 1
Topic Options
#82699 - 01/12/21 05:55 PM EndProc() - Terminate a process using WMI
Kdyer Moderator Offline
Moderator
*****

Registered: 01/01/03
Posts: 6240
Loc: Tigard, OR
Code:

;NAME: ENDPROC
;AUTHOR: Kent Dyer (leptonator@hotmail.com)
;CONTRIBUTORS: Conrad Wheeler "Radimus", kholm
;ACTION: Terminates a local or remote process using WMI
;SYNTAX: ENDPROC($PROC,optional $COMPUTER)
;VERSION: 1.2
;PARAMETERS: $PROC
; Required string for the name of process you want to kill
; $COMPUTER
; Optional string containing the name of the system you are interested in,
; you could use @WKSTA
;RETURNS: Returncode (0) and PID
;REMARKS: Reference - Terminate all copies of the& nbsp;Notepad process on host TEST01
; Simplified (hopefully) version of kholm's script
; No warning about program being killed.
;DEPENDENCIES: Kixtart 4.0, WMI
;EXAMPLE(s): $COMPUTER = @WKSTA
; $PROC = "NOTEPAD.EXE"
;
; ENDPROC($PROC,$COMPUTER)
;
; Returns:
; 0
; notepad.exe is 1508
FUNCTION ENDPROC($PROC, optional $COMPUTER)
DIM $Process
IF $COMPUTER=''
$COMPUTER='.'
ENDIF
For each $Process in GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$COMPUTER+'\root\cimv2')
.ExecQuery('Select * from Win32_Process where Name=' +'"'+$Proc+'"')
$Process.Terminate
?$Process.Name+' is '+$Process.ProcessId
Next
ENDFUNCTION


Another way to do this and suppress the results:

Code:

FUNCTION ENDPROC($PROC, optional $COMPUTER)
Dim $Process,$RC
IF $COMPUTER=''
$COMPUTER='.'
ENDIF
For each $Process in GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$COMPUTER+'\root\cimv2')
.ExecQuery('select * from Win32_Process where Name='+'"'+$PROC+'"')
$RC=$Process.Terminate
Next
ENDFUNCTION


_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#82700 - 01/12/22 12:47 AM Re: EndProc() - Terminate a process using WMI
Radimus Moderator Offline
Moderator
*****

Registered: 00/01/06
Posts: 5163
Loc: Tampa, FL
ahhh... I can hear the lamentations of the users as AIM and AOL are ripped from their machines....

Good Job

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#82701 - 02/06/19 01:52 AM Re: EndProc() - Terminate a process using WMI
Radimus Moderator Offline
Moderator
*****

Registered: 00/01/06
Posts: 5163
Loc: Tampa, FL
here is a modifid version

code:
;**************************************************************************
FUNCTION FindPROC($PROC,optional $COMPUTER, optional $terminate)
dim $GetObject, $Select
if not $computer $computer=@wksta endif
$GetObject="winmgmts:{impersonationLevel=impersonate}!//$COMPUTER"
$select="select * from Win32_Process where Name='$PROC'"
For each $Process in GetObject("$GetObject").ExecQuery("$select")
if $terminate $Process.Terminate endif
$FindPROC=$Process.ProcessId
Next
ENDFUNCTION



[ 19 June 2002, 01:54: Message edited by: Radimus ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#82702 - 03/09/01 12:00 AM Re: EndProc() - Terminate a process using WMI
NTDOC Administrator Offline
Administrator
*****

Registered: 00/07/28
Posts: 11064
Loc: CA
Kent and Rad

I think you may have the slashes wrong and possibly the quotes.

Using the UDF as supplied does not work when you also use
$nul=SetOption('NoVarsInStrings','ON')

Here is a modification that appears to work when using the strict settings


Break On
$nul=SetOption('WrapAtEOL','ON')
$nul=SetOption('Explicit','ON')
$nul=SetOption('NoVarsInStrings','ON')

DIM $wksta,$EndAIM
$wksta = @WKSTA
$EndAIM = ENDPROC($wksta,'aim.exe')

FUNCTION ENDPROC($wksta,$proc)
DIM $Process,$EndAIM
For each $Process in GetObject('WinMgmts:'+'{impersonationLevel=impersonate}!\\'+$wksta+'\root\cimv2').ExecQuery('select * from Win32_process where name="AIM.EXE"')
$EndAIM = $Process.Terminate()
Next
ENDFUNCTION

Top
#82703 - 03/08/31 01:47 PM Re: EndProc() - Terminate a process using WMI
Radimus Moderator Offline
Moderator
*****

Registered: 00/01/06
Posts: 5163
Loc: Tampa, FL
I've moved on to a more robust and verbose one..

http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000305
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#82704 - 03/08/31 06:29 PM Re: EndProc() - Terminate a process using WMI
Radimus Moderator Offline
Moderator
*****

Registered: 00/01/06
Posts: 5163
Loc: Tampa, FL
perhaps robust isn't the right term... a more selective terminator.

instead of killing all $apps, it can kill a specific pid
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#82705 - 03/09/02 05:32 AM Re: EndProc() - Terminate a process using WMI
Kdyer Moderator Offline
Moderator
*****

Registered: 01/01/03
Posts: 6240
Loc: Tigard, OR
Code is corrected..

I am using this "out on the floor" right now to kill an NTVDM process that is not be terminated properly.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#82706 - 03/09/02 06:27 PM Re: EndProc() - Terminate a process using WMI
NTDOC Administrator Offline
Administrator
*****

Registered: 00/07/28
Posts: 11064
Loc: CA
Kent,

Have not retried your code, but does it now properly support remote systems with this set in the script?

$nul=SetOption('NoVarsInStrings','ON')


Also, please review the PostPrep code. It has a few errors in it currently.

Thanks Kent.

[ 02. September 2003, 18:30: Message edited by: NTDOC ]

Top
#82707 - 03/09/02 06:29 PM Re: EndProc() - Terminate a process using WMI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 01/06/05
Posts: 20439
Loc: FI
doc, no.
btw kent, is "[/CODE]" new kixtart keyword?

[ 02. September 2003, 18:32: Message edited by: Lonkero ]

Top
#82708 - 03/10/02 02:47 PM Re: EndProc() - Terminate a process using WMI
Kdyer Moderator Offline
Moderator
*****

Registered: 01/01/03
Posts: 6240
Loc: Tigard, OR
sheesh.. jlo - [CODE] ... [/CODE]

has been removed.. My bad

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#82709 - 03/10/02 03:45 PM Re: EndProc() - Terminate a process using WMI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 01/06/05
Posts: 20439
Loc: FI
the var in string still remains [Wink]
I would suggest something like this, to get the errorcodes too if wanted:
FUNCTION ENDPROC($COMPUTER,$PROC,optional $rc)
Dim $
IF $COMPUTER=''
$COMPUTER='.'
ENDIF
For each $ in GetObject("winmgmts:{impersonationLevel=impersonate}!\\"+$COMPUTER).ExecQuery("select * from Win32_Process where Name='"+$PROC+"'")
$=$.Terminate
if vartype($rc)
redim preserve $endproc[ubound($endproc)+1]
$endproc[ubound($endproc)]=$
endif
Next
ENDFUNCTION

Top
#82710 - 03/10/15 07:00 PM Re: EndProc() - Terminate a process using WMI
Fred_Leonard Offline
Fresh Scripter

Registered: 03/08/21
Posts: 14
Loc: Medford, OR
Doesn't work on all apps/pcoc.

I have tested, and it does work well on calc and notepad, so I know the script works, but there is a particular process I need to kill, and I cannot.

The kill process from the server sesource kit kills it with a force switch.

So, is there any way to add a FORCE flag?

Maybe it can't be done with WMI, I don't know.

Any thoughts on this?

[ 15. October 2003, 19:01: Message edited by: Fred_Leonard ]
_________________________
Fred Leonard

Top
#82711 - 03/10/16 09:25 AM Re: EndProc() - Terminate a process using WMI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 01/06/05
Posts: 20439
Loc: FI
ja.
think like the name says, this is "soft" kill.
you can as example kill services with this like with the real kill.
(something I do when get mad, kill -f "svchost.exe")

not sure if there is force as there is no force in taskmanager either.

Top
#82712 - 04/10/30 10:04 AM Re: EndProc() - Terminate a process using WMI
NTDOC Administrator Offline
Administrator
*****

Registered: 00/07/28
Posts: 11064
Loc: CA
Kent,

Can you please update your post to either Jooel's code or this code, or some other code that does support NoVarInStrings.

You post is the only version that shows on the UDF mirrors which currently does not support this option.

NOTE: Jooel's code currently has an error in it in the fact that the IF $COMPUTER does not work as it can not be blank since it is not optional at the moment.


Code:
Function EndProc($proc, optional $strComputer)
DIM $Process
If $strComputer=''
$strComputer='.'
EndIf
For Each $Process In GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2").ExecQuery("Select * from Win32_Process where Name= " +'"'+$Proc+'"')
$Process=$Process.Terminate
Next
EndFunction


Top
#82713 - 04/11/24 09:50 AM Re: EndProc() - Terminate a process using WMI
Kdyer Moderator Offline
Moderator
*****

Registered: 01/01/03
Posts: 6240
Loc: Tigard, OR
Doc,

Thanks for this information. Sorry for the delay in getting it fixed. This has been completed.

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
Page 1 of 1 1


Moderator:  Jochen, Radimus, Sealeopard, Bryce, Kdyer, Howard Bullock, Chris S., Glenn Barnas, Allen, Benny69, Mart 
Hop to:
Shout Box

Who's Online
5 registered (Mart, Glenn Barnas, Sam_B, Digi, Mark_Shaw) and 60 anonymous users online.

Generated in 6.642 seconds in which 2.823 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org