1
我創建這個代碼來幫助我開發一個更大的進程。這件作品將幫助我實現自動化。假設你已經打開至少2個窗口,下面的第一部分找到第一個打開的時間並將其設置爲變量「strReturn」。這基本上是刪除冒號的時間。我刪除了更容易進行數字比較的冒號。由於它似乎是按順序循環的,所以我立即退出FOR語句,因爲第一個是我所關心的。殺死一個進程的所有實例,除了最老的(第一個)實例(VBScript)
然後,第二個語句是爲了終止一個進程的所有實例而構建的,我試圖修改爲只殺掉一個匹配變量中時間的語句。但是,它什麼也沒做......我確定我錯誤地實施了它。我粗體顯示了我修改過的部分,並試圖大膽 - 你會看到星號......哈哈。是否清楚我要去哪裏? TIA
'=============================================================
'Finds time first instance of iexplore process was started
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'iexplore.exe'")
For Each objProcess in colProcessList
dtmStartTime = objProcess.CreationDate
strReturn = replace(split(WMIDateStringToDate(dtmStartTime), " ")(1), ":", "")
wscript.echo strReturn
exit for
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 the instance matching the 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 * From Win32_Process", , 48)
For Each objProcess in colProcesses
If LCase(myProcess) = LCase(objProcess.Name) **and strReturn < replace(split(WMIDateStringToDate(dtmStartTime), " ")(1), ":", "")** Then 'Confirm process is running
blnRunning = True 'Get exact case for the actual process name
myProcess = objProcess.Name 'Kill process
objProcess.Terminate()
End If
Next
If blnRunning Then
Do Until Not blnRunning
Set colProcesses = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("Select * From Win32_Process Where Name = '"& myProcess & "'")
WScript.Sleep 1000 'Wait 1 second
If colProcesses.Count = **1** Then 'Exit loop (changed from 0 to 1)
blnRunning = False
End If
Loop
End If
End Sub