我的程序问题有时无法正常退出。我仍然在努力修复它,但我现在需要一种解决方法来终止这个过程。
问题是我不能用以下命令杀死它:
taskkill /PID <pid> /T /F
我有一个错误:“没有正在运行的任务实例”
但是,可以通过打开任务管理器并从此处停止进程来终止它。
所以我想知道任务管理器发送了什么命令来结束可以在脚本中使用的进程?或者也许是另一种方法?
编辑:问题的图片
用法语写的,翻译是:
Error : process with PID 9664 <child of process with PID 4920> can't be stopped
Cause : There is no running instance of the task
答案 0 :(得分:1)
vbscript.vbs
task="process.exe"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & task & "'",,48)
For Each objItem in colItems
'Wscript.Echo "-------------------------------------------"
'Wscript.Echo "CommandLine: " & objItem.CommandLine
'Wscript.Echo "Name: " & objItem.Name
'Wscript.Echo "PID: " & objItem.ProcessId
'Wscript.Echo "StartTime: " & objItem.CreationDate
StartTime=objItem.CreationDate
Y=Mid(StartTime,1,4)
Mo=mid(StartTime,5,2)
D=mid(StartTime,7,2)
H=mid(StartTime,9,2)
M=mid(StartTime,11,2)
S=mid(StartTime,13,2)
'Wscript.Echo M & "/" & D & "/" & Y & " " & H & ":" & M & ":" & S
Q=Cdate(Mo & "/" & D & "/" & Y & " " & H & ":" & M & ":" & S)
elapsed=DateDiff("s", Q, Now())
'Wscript.Echo Now() & vbtab & Q & vbtab & elapsed
'If process is older then 1 min then terminate
If elapsed> 60 and lcase(objItem.Name) ="cscript.exe" then
Wscript.Echo "Terminate:" & " " & objItem.CreationDate & " " & objItem.CommandLine
objItem.Terminate()
End if
Next