2016-07-29 24 views
1

因此,對我的業務和員工至關重要的應用程序是我們的時鐘。它是多年前設計的一個更大的RMS系統的exe部分。當您進入或退出時,應用程序會自動關閉。我創建了一個桌面快捷方式,以便更快地打開,但我想知道是否有辦法用Windows腳本來完成此操作。Windows腳本(VBS)總是使應用程序保持開放

基本參數:

在計算機啓動時,腳本運行和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""") 

回答

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

所以

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

此運行'Program.exe'在infinte洛op ??? – Hackoo

+1

程序退出時,腳本會重新啓動它。 – 2016-07-29 07:58:26

+0

啊好吧,我現在看到它!聰明的想法! – Hackoo

3

相反投票詢問是否它的運行,跟蹤事件時引發進程停下來,觀看clock.exe停止和重新啓動的。這應該使用更少的資源(「告訴我,當我們到達那裏」與「我們差不多了嗎?我們差不多了嗎?我們差不多了嗎?」),並且響應更快(不用等待下一個輪詢開始)。

' 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改編)

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 
'************************************************************************** 
相關問題