以下vbs代码段实现在特定时间将指定的进程kill掉:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Usage: ' ' cscript AutoTerminateTicCopyBat.vbs >> terminatecopy.log ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Global Constants Initialization ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' COMMAND_TO_TERMINATE = "copy-data.bat" HOUR_OF_ENDTIME_IN_GMT8 = 16 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Terminates the specific process during the given time range ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function TerminatesCmdProcess(strProcess) Dim objProcesses strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name ='cmd.exe' and "_ & " CommandLine like '%" & COMMAND_TO_TERMINATE & "%'") For Each objProcess in colProcesses objProcess.Terminate() WScript.Echo Now() & " Automatically terminated the process " & objProcess.CommandLine Next End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Check if it is the time to terminate the specific process ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function IsTime2Terminate Dim bRet Dim hourOfCurUtcTime Dim hourOfCurTimeInGMT8 bRet = False 'Get current UTC time on the machine strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_UTCTime") For Each objItem in colItems hourOfCurUtcTime = objItem.Hour hourOfCurTimeInGMT8 = hourOfCurUtcTime + 8 Next If hourOfCurTimeInGMT8 >= HOUR_OF_ENDTIME_IN_GMT8 Then bRet = True End If IsTime2Terminate = bRet End Function 'Main Process Dim bFlag bFlag = True Do While bFlag If IsTime2Terminate() Then TerminatesCmdProcess strProcess End If WScript.Sleep 1000*60 Loop