1. ホーム
  2. スクリプト・コラム
  3. vbs

指定されたプロセス名の小さなVBSを終了させる

2022-02-08 10:31:48

指定したプロセス名を強制終了する小さなvbsです、お役に立てれば幸いです。

Function KillProc(strProcName)
On Error Resume Next
 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}! \\\. \root\cimv2")
 Set arrProcesses = objWMIService.ExecQuery( "select * from win32_process where Name ='"&strProcName&"'" )
 For Each proccess In arrProcesses
 Terminate 0 
 Terminate 0
End Function

VBSコマンド-プロセス操作コード(プロセス検出、プロセス終了)

//Detect process
Process name = "qq.exe"
Return value = IsProcess(process name)
If return value = True Then
MessageBox "Process found"
ElseIf Return Value = False Then
MessageBox "No process found"
End If
//Detect process Optimized code
If IsProcess("qq.exe") = True Then 
MessageBox "Process found"
Else 
MessageBox "No process found"
End If
//Detect process group
Process group = "qq.exe|notepad.exe"
Return Value = IsProcessEx(process group)
If return value = True Then
MessageBox "Found process"
ElseIf Return Value = False Then
MessageBox "No process found"
End If
//Detect process group Optimized code
If IsProcessEx("qq.exe|notepad.exe") = True Then 
MessageBox "Process found"
Else 
MessageBox "No process found"
End If
//end process foreground execution
Process name = "qq.exe"
Call CloseProcess(process name, 1)
//end the process Background execution
Process name = "qq.exe"
Call CloseProcess(process name, 0)
//end process group foreground execution
Process group = "qq.exe|notepad.exe"
Call CloseProcessEx(process group, 1)
//end the process group Background execution
Process group = "qq.exe|notepad.exe"
Call CloseProcessEx(process group, 0)
//Example application End process foreground execution 10 seconds timeout
Process name = "qq.exe"
For 10
Call CloseProcess(process name, 1)
Delay 1000
Return Value = IsProcess(process name)
If Return Value = False Then
Exit For
End If
Next
If Return Value = True Then
MessageBox "Failed to end process"
Else
MessageBox "End process successfully"
End If
//Example application End the process Execute the optimized code in the foreground (until the loop) Some processes are not detected by VBS, so close them first and detect them later.
Do
Call CloseProcess("qq.exe",1)
Delay 1000
Loop While IsProcess("qq.exe")=True
MessageBox "End process successfully"
//Example application End process group Background execution 10 seconds timeout
Process group = "qq.exe|notepad.exe"
For 10
Call CloseProcessEx(process group,0)
Delay 1000
Return Value = IsProcessEx(process group)
If Return Value = False Then
Exit For
End If
Next
If Return Value = True Then
MessageBox "Failed to end process"
Else
MessageBox "End process successfully"
End If
//Example application End process group Execute optimized code in background (until loop) Some processes are not detected by VBS so close first and detect later
Do
Call CloseProcessEx( "qq.exe|notepad.exe",0)
Delay 1000
Loop While IsProcessEx( "qq.exe|notepad.exe")=True
MessageBox "End process successfully"
// function subroutine part of the code
//Detect the process
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
//End the process
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
//Detect process group
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
//End the process group
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