Kill all instances of a process except oldest (1st) instance (vbscript) -
i'm creating code aid in bigger process i've developed. piece me automate it. assuming have opened @ least 2 ie windows, first section below finds time 1st opened , set variable "strreturn". time colons removed. removed colons easier number comparison. since seems loop through these in order, have exit statement since first care about.
then, second statement, built kill instances of process, attempted modify kill 1 matches time in variable. however, nothing... i'm sure i'm implementing incorrectly. i've bolded parts i've modified original..well, attempted bold - you'll see asterisks...haha. clear i'm going wrong? tia
'============================================================= 'finds time first instance of iexplore process started strcomputer = "." set objwmiservice = getobject("winmgmts:\\" & strcomputer & "\root\cimv2") set colprocesslist = objwmiservice.execquery _ ("select * win32_process name = 'iexplore.exe'") each objprocess in colprocesslist dtmstarttime = objprocess.creationdate strreturn = replace(split(wmidatestringtodate(dtmstarttime), " ")(1), ":", "") wscript.echo strreturn exit next '............................................................. function wmidatestringtodate(dtmstart) wmidatestringtodate = cdate(mid(dtmstart, 5, 2) & "/" & _ mid(dtmstart, 7, 2) & "/" & left(dtmstart, 4) _ & " " & mid (dtmstart, 9, 2) & ":" & _ mid(dtmstart, 11, 2) & ":" & mid(dtmstart, _ 13, 2)) end function '============================================================= '(supposed to) kill every instance of iexplore.exe except instance matching start time in strreturn variable (1st instance). killproc "iexplore.exe" sub killproc( myprocess ) dim blnrunning, colprocesses, objprocess blnrunning = false set colprocesses = getobject("winmgmts:{impersonationlevel=impersonate}").execquery( "select * win32_process", , 48 ) each objprocess in colprocesses if lcase( myprocess ) = lcase( objprocess.name ) **and strreturn < replace(split(wmidatestringtodate(dtmstarttime), " ")(1), ":", "")** 'confirm process running blnrunning = true 'get exact case actual process name myprocess = objprocess.name 'kill process objprocess.terminate() end if next if blnrunning until not blnrunning set colprocesses = getobject("winmgmts:{impersonationlevel=impersonate}").execquery( "select * win32_process name = '"& myprocess & "'" ) wscript.sleep 1000 'wait 1 second if colprocesses.count = **1** 'exit loop (changed 0 1) blnrunning = false end if loop end if end sub
i got work. overthinking it, tend do...
'============================================================= 'finds time first instance of iexplore process started strcomputer = "." set objwmiservice = getobject("winmgmts:\\" & strcomputer & "\root\cimv2") set colprocesslist = objwmiservice.execquery _ ("select * win32_process name = 'iexplore.exe'") each objprocess in colprocesslist dtmstarttime = objprocess.creationdate strreturn = replace(split(wmidatestringtodate(dtmstarttime), " ")(1), ":", "") exit next each objprocess in colprocesslist dtmstarttime = objprocess.creationdate strreturn1 = replace(split(wmidatestringtodate(dtmstarttime), " ")(1), ":", "") if strreturn <> strreturn1 objprocess.terminate() end if next '............................................................. function wmidatestringtodate(dtmstart) wmidatestringtodate = cdate(mid(dtmstart, 5, 2) & "/" & _ mid(dtmstart, 7, 2) & "/" & left(dtmstart, 4) _ & " " & mid (dtmstart, 9, 2) & ":" & _ mid(dtmstart, 11, 2) & ":" & mid(dtmstart, _ 13, 2)) end function '=============================================================
Comments
Post a Comment