Option Explicit
Dim wmiLocator
Dim wmiService
Dim objEnumerator
Dim strQuery
strQuery = "Select * FROM Win32_Process WHERE (Caption = 'wscript.exe' OR " & _
"Caption = 'cscript.exe') AND CommandLine LIKE '%" & WScript.ScriptName & "%'"
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
Set wmiService = wmiLocator.ConnectServer
Set objEnumerator = wmiService.ExecQuery(strQuery)
If objEnumerator.Count > 1 Then
WScript.Echo "同じスクリプトが実行されています。"
Else
WScript.Sleep 10000
End If
WScript.Echo "end"
作成 2013.11.25
更新 2013.11.25
更新 2013.11.25
VBScript の多重起動を検出する
コード
process-finddup.vbs
実行結果
同じスクリプトが起動していない場合は 10秒間停止します。
C:\>cscript process-finddup.vbs Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. end同じスクリプトがすでに起動していた場合は即座に終了します。
C:\>cscript process-finddup.vbs Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. 同じスクリプトが実行されています。 end
タグ: VBScript