vbs核心代碼
1
2
3
4
5
6
7
8
9
10
11
12
|
Option Explicit Dim objWMIService,colProcessList,strComputer strComputer = "." Set objWMIService = GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2" ) Set colProcessList = objWMIService.ExecQuery( "Select * from Win32_Process Where Name = 'excel.exe'" ) If colProcessList.Count>0 Then MsgBox "檢測到EXCEL程序運行中,程序退出!" WScript.Quit End If Set colProcessList = Nothing Set objWMIService = Nothing WScript.Quit |
當然你可以判斷 winrar.exe等等
下面附一個代碼,原來中文命名的,服務器之家已經修改為英文命名并且正常運行了,因為時間問題,需要的朋友可以自行修改精簡
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
'檢測進程 proname = "qq.exe" reName = IsProcess(proname) If reName = True Then msgbox "發現進程" ElseIf reName = False Then msgbox "沒有發現進程" End If '檢測進程 優化后的代碼 If IsProcess( "qq.exe" ) = True Then msgbox "發現進程" Else msgbox "沒有發現進程" End If '檢測進程組 proName_all = "qq.exe|notepad.exe" reName = IsProcessEx(proName_all) If reName = True Then msgbox "發現進程" ElseIf reName = False Then msgbox "沒有發現進程" End If '檢測進程組 優化后的代碼 If IsProcessEx( "qq.exe|notepad.exe" ) = True Then msgbox "發現進程" Else msgbox "沒有發現進程" End If '結束進程 前臺執行 proname = "qq.exe" Call CloseProcess(proname, 1) '結束進程 后臺執行 proname = "qq.exe" Call CloseProcess(proname, 0) '結束進程組 前臺執行 proName_all = "qq.exe|notepad.exe" Call CloseProcessEx(proName_all, 1) '結束進程組 后臺執行 proName_all = "qq.exe|notepad.exe" Call CloseProcessEx(proName_all, 0) '實例應用 結束進程 前臺執行 10秒超時 proname = "qq.exe" For i=1 to 10 Call CloseProcess(proname,1) Delay 1000 reName = IsProcess(proname) If reName = False Then Exit For End If Next If reName= True Then msgbox "結束進程失敗" Else msgbox "結束進程成功" End If '實例應用 結束進程 前臺執行 優化后的代碼(直到型循環) 有些進程VBS檢測不到 所以先關閉后檢測 Do Call CloseProcess( "qq.exe" ,1) Delay 1000 Loop While IsProcess( "qq.exe" )= True msgbox "結束進程成功" '實例應用 結束進程組 后臺執行 10秒超時 proName_all = "qq.exe|notepad.exe" For j=1 to 10 Call CloseProcessEx(proName_all,0) Delay 1000 reName = IsProcessEx(proName_all) If reName = False Then Exit For End If Next If reName= True Then msgbox "結束進程失敗" Else msgbox "結束進程成功" End If '實例應用 結束進程組 后臺執行 優化后的代碼(直到型循環) 有些進程VBS檢測不到 所以先關閉后檢測 Do Call CloseProcessEx( "qq.exe|notepad.exe" ,0) Delay 1000 Loop While IsProcessEx( "qq.exe|notepad.exe" )= True msgbox "結束進程成功" '函數 子程序部分代碼 '檢測進程 Function IsProcess(ExeName) Dim WMI, Obj, Objs,i IsProcess = False Set WMI = GetObject( "WinMgmts:" ) Set Objs = WMI.InstancesOf( "Win32_Process" ) For Each Obj In Objs If InStr(UCase(ExeName),UCase(Obj.Description)) <> 0 Then IsProcess = True Exit For End If Next Set Objs = Nothing Set WMI = Nothing End Function '結束進程 Sub CloseProcess(ExeName,RunMode) dim ws Set ws = createobject( "Wscript.Shell" ) ws.run "cmd.exe /C Taskkill /f /im " & ExeName,RunMode Set ws = Nothing End Sub '檢測進程組 Function IsProcessEx(ExeName) Dim WMI, Obj, Objs,ProcessName,i IsProcessEx = False Set WMI = GetObject( "WinMgmts:" ) Set Objs = WMI.InstancesOf( "Win32_Process" ) ProcessName=Split(ExeName, "|" ) For Each Obj In Objs For i=0 to UBound(ProcessName) If InStr(UCase(ProcessName(i)),UCase(Obj.Description)) <> 0 Then IsProcessEx = True Exit For End If Next Next Set Objs = Nothing Set WMI = Nothing End Function '結束進程組 Sub CloseProcessEx(ExeName,RunMode) dim ws,ProcessName,CmdCode,i ProcessName = Split(ExeName, "|" ) For i=0 to UBound(ProcessName) CmdCode=CmdCode & " /im " & ProcessName(i) Next Set ws = createobject( "Wscript.Shell" ) ws.run "cmd.exe /C Taskkill /f" & CmdCode,RunMode Set ws = Nothing End Sub |
好了這篇關于vbs進程判斷的文章就介紹到這