始终使应用程序保持打开状态的Windows脚本(VBS)

始终使应用程序保持打开状态的Windows脚本(VBS),windows,shell,batch-file,vbscript,Windows,Shell,Batch File,Vbscript,因此,对我的业务和员工至关重要的应用程序是我们的时钟。它是多年前设计的大型RMS系统的一部分。当您输入或输出时钟时,应用程序将自动关闭。我创建了一个桌面快捷方式以加快打开速度,但我想知道是否有一种方法可以使用Windows脚本来实现这一点 基本参数: 启动计算机后,将运行脚本并打开clock.exe应用程序 如果在任何时候,clock.exe关闭或停止;脚本将重新打开clock.exe 我自己搞乱了脚本,并设法让脚本打开应用程序,但我尝试使用一个循环让它监视这个过程,这导致许多时钟应用程序打开,

因此,对我的业务和员工至关重要的应用程序是我们的时钟。它是多年前设计的大型RMS系统的一部分。当您输入或输出时钟时,应用程序将自动关闭。我创建了一个桌面快捷方式以加快打开速度,但我想知道是否有一种方法可以使用Windows脚本来实现这一点

基本参数:

启动计算机后,将运行脚本并打开clock.exe应用程序

如果在任何时候,clock.exe关闭或停止;脚本将重新打开clock.exe

我自己搞乱了脚本,并设法让脚本打开应用程序,但我尝试使用一个循环让它监视这个过程,这导致许多时钟应用程序打开,最后由于过载导致计算机关闭

option explicit
DIM strComputer,strProcess

strComputer = "." ' local computer
strProcess = "clock.exe"

' Check if Calculator is running on specified computer (. = local computer)

if isProcessRunning(strComputer,strProcess) then
CreateObject("WScript.Shell").Run("""C:\Users\FrontDesk1\Documents\alwaysClock.vbs""")
else
CreateObject("WScript.Shell").Run("""C:\RMS\pos\clock.exe""")
end if

' Function to check if a process is running
function isProcessRunning(byval strComputer,byval strProcessName)

Dim objWMIService, strWMIQuery

strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
        & strComputer & "\root\cimv2") 


if objWMIService.ExecQuery(strWMIQuery).Count > 0 then
    isProcessRunning = true
else
    isProcessRunning = false
end if

end function

CreateObject("WScript.Shell").Run("""C:\Users\FrontDesk1\Documents\alwaysClock.vbs""")
所以

所以


不要轮询询问它是否正在运行,而是跟踪进程停止时引发的事件,观察clock.exe是否停止并重新启动它。这应该使用更少的资源(“告诉我我们什么时候到了”vs.“我们快到了吗?我们快到了吗?我们快到了吗?”),并更快地做出响应(无需等待下一次投票开始)


(改编自)

不要轮询询问它是否正在运行,而是跟踪进程停止时引发的事件,观察clock.exe是否停止并重新启动它。这应该使用更少的资源(“告诉我我们什么时候到了”vs.“我们快到了吗?我们快到了吗?我们快到了吗?”),并更快地做出响应(无需等待下一次投票开始)


(改编自)

您可以从Chancity

Option Explicit
If AppPrevInstance() Then   
    MsgBox "Instance already running",VbExclamation,"Instance already running"    
    WScript.Quit   
Else   
    Do   
        Call Main(Array("c:\toto1.bat","c:\toto2.bat","c:\toto3.bat","%ProgramFiles%\Internet Explorer\iexplore.exe"))
        Call Pause(15) 'Pause de 15 minutes
    Loop   
End If   
'**************************************************************************
Sub Main(colProcessPaths)   
    Dim ProcessPath   
    For Each ProcessPath In colProcessPaths     
        CheckProcess(ProcessPath)   
    Next   
End Sub   
'**************************************************************************
Sub CheckProcess(ProcessPath)   
    Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))   
            If .Count = 0 Then    
                With CreateObject("WScript.Shell")  
                    .Run DblQuote(ProcessPath) 
                End With    
            Else    
                Exit Sub    
            End if   
        End With   
    End With   
End Sub   
'**************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
        " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function   
'**************************************************************************
Sub Pause(Minutes)    
    Wscript.Sleep(Minutes*1000*60)    
End Sub   
'**************************************************************************
Function StripProcPath(ProcessPath)   
    Dim arrStr : arrStr = Split(ProcessPath, "\")   
    StripProcPath = arrStr(UBound(arrStr))   
End Function   
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

您可以从Chancity

Option Explicit
If AppPrevInstance() Then   
    MsgBox "Instance already running",VbExclamation,"Instance already running"    
    WScript.Quit   
Else   
    Do   
        Call Main(Array("c:\toto1.bat","c:\toto2.bat","c:\toto3.bat","%ProgramFiles%\Internet Explorer\iexplore.exe"))
        Call Pause(15) 'Pause de 15 minutes
    Loop   
End If   
'**************************************************************************
Sub Main(colProcessPaths)   
    Dim ProcessPath   
    For Each ProcessPath In colProcessPaths     
        CheckProcess(ProcessPath)   
    Next   
End Sub   
'**************************************************************************
Sub CheckProcess(ProcessPath)   
    Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))   
            If .Count = 0 Then    
                With CreateObject("WScript.Shell")  
                    .Run DblQuote(ProcessPath) 
                End With    
            Else    
                Exit Sub    
            End if   
        End With   
    End With   
End Sub   
'**************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
        " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function   
'**************************************************************************
Sub Pause(Minutes)    
    Wscript.Sleep(Minutes*1000*60)    
End Sub   
'**************************************************************************
Function StripProcPath(ProcessPath)   
    Dim arrStr : arrStr = Split(ProcessPath, "\")   
    StripProcPath = arrStr(UBound(arrStr))   
End Function   
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

当程序退出时,脚本将重新启动它。啊,好的;,我现在看到了!好主意!当程序退出时,脚本将重新启动它。啊,好的;,我现在看到了!好主意!
' Monitor process stop trace events.

Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colProcessStopTrace = objWMIService.ExecNotificationQuery _
            ("SELECT * FROM Win32_ProcessStopTrace")

Do
    Set objLatestEvent = colProcessStopTrace.NextEvent
    Wscript.Echo "Test message: process stopped: " & objLatestEvent.ProcessName

    if objLatestEvent.ProcessName = "clock.exe" then
        CreateObject("WScript.Shell").Run("""C:\RMS\pos\clock.exe""")
    end if
Loop
Option Explicit
If AppPrevInstance() Then   
    MsgBox "Instance already running",VbExclamation,"Instance already running"    
    WScript.Quit   
Else   
    Do   
        Call Main(Array("c:\toto1.bat","c:\toto2.bat","c:\toto3.bat","%ProgramFiles%\Internet Explorer\iexplore.exe"))
        Call Pause(15) 'Pause de 15 minutes
    Loop   
End If   
'**************************************************************************
Sub Main(colProcessPaths)   
    Dim ProcessPath   
    For Each ProcessPath In colProcessPaths     
        CheckProcess(ProcessPath)   
    Next   
End Sub   
'**************************************************************************
Sub CheckProcess(ProcessPath)   
    Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))   
            If .Count = 0 Then    
                With CreateObject("WScript.Shell")  
                    .Run DblQuote(ProcessPath) 
                End With    
            Else    
                Exit Sub    
            End if   
        End With   
    End With   
End Sub   
'**************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
        " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function   
'**************************************************************************
Sub Pause(Minutes)    
    Wscript.Sleep(Minutes*1000*60)    
End Sub   
'**************************************************************************
Function StripProcPath(ProcessPath)   
    Dim arrStr : arrStr = Split(ProcessPath, "\")   
    StripProcPath = arrStr(UBound(arrStr))   
End Function   
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************