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
| Option Explicit
Dim ProcessPath
ProcessPath = "c:\toto.bat"
'Quitter si le script est déjà lancé
If AppPrevInstance() = True Then WScript.Quit
'**************************************************************************
'Une boucle Do...Loop avec une pause de 15 minutes
Do
Call CheckProcess(DblQuote(ProcessPath))
Call Pause(15) 'Pause de 15 minutes
Loop
'**************************************************************************
Sub CheckProcess(ProcessPath)
Dim strComputer,objWMIService,colProcesses,WshShell,Tab,ProcessName
strComputer = "."
Tab = Split(ProcessPath,"\")
ProcessName = Tab(UBound(Tab))
ProcessName = Replace(ProcessName,Chr(34),"")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '"& ProcessName & "'")
If colProcesses.Count = 0 Then
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run ProcessPath
Else
Exit Sub
End if
End Sub
'**************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Sub Pause(NMinutes)
Wscript.Sleep(NMinutes*1000*60)
End Sub
'**************************************************************************
Function AppPrevInstance()
'Vérifie si un script portant le même nom que le présent script est déjà lancé
Dim strComputer,objWMIService,colScript,objScript,RunningScriptName,Counter
strComputer = "."
Counter = 0
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set ColScript = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Wscript.exe' OR Name = 'Cscript.exe'")
For Each objScript In colScript
RunningScriptName = Mid(objScript.CommandLine, InstrRev(objScript.CommandLine, "\", -1, 1) + 1, Len(objScript.CommandLine) - InstrRev(objScript.CommandLine, "\", -1, 1) - 2)
If WScript.ScriptName = RunningScriptName Then Counter = Counter + 1
Wscript.Sleep 10000
Next
If Counter > 1 Then
AppPrevInstance = True
Else
AppPrevInstance = False
End If
Set colScript = Nothing
Set objWMIService = Nothing
End Function
'************************************************************************** |
Partager