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

时间:2016-07-29 03:10:44

标签: windows shell batch-file vbscript

因此,对我的业务和员工至关重要的应用程序是我们的时钟。它是多年前设计的更大RMS系统的exe部分。时钟输入或输出时,应用程序自动关闭。我创建了一个桌面快捷方式,以便更快地打开,但我想知道是否有办法使用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""")

3 个答案:

答案 0 :(得分:3)

不是轮询询问它是否正在运行,而是跟踪进程停止时引发的事件,注意clock.exe停止并重新启动它。这应该使用更少的资源("告诉我什么时候到达那里" vs."我们差不多了吗?我们差不多了吗?我们差不多了吗?" ),并且响应更快(无需等待下一轮调查开始)。

' 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

(改编自http://www.vbsedit.com/scripts/misc/events/scr_1218.asp

答案 1 :(得分:1)

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn]) 

所以

Set oShell = WScript.CreateObject ("WSCript.shell")
Do
    oShell.Run "C:\Folder\Program.exe", 1, true
Loop

答案 2 :(得分:0)

您可以从 Chancity

的代码中获得灵感

A Vbscript to checks the launch of a .bat and .exe programs

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
'**************************************************************************