2015-12-01 24 views
1

道歉我在vbs上有點新手,並想知道我是否可以得到一些幫助。檢查進程是否正在運行VBS,何時關閉註銷

我想寫一些vbscript來檢查進程名是否仍在運行,如果它正在檢查進程。如果進程名稱未運行註銷窗口。

這是我到目前爲止已經試過

set service = GetObject ("winmgmts:") 

for each Process in Service.InstancesOf ("Win32_Process") 
    If Process.Name = "notepad.exe" False ,then 

Set WshShell=WScript.CreateObject("WScript.Shell") 
    WshShell.run "shutdown.exe -L -F" 
    'wscript.echo "Notepad running" 
    'wscript.quit 
End If 
next 

請讓我知道如果你需要任何更多信息:)

回答

2

這VBScript中可以做的伎倆:

Option Explicit 
Dim ProcessPath,WshShell 
ProcessPath = "%Windir%\System32\Notepad.exe" 
Set WshShell = CreateObject("WScript.Shell") 
If AppPrevInstance() Then 
    MsgBox "There is an existing proceeding !" & VbCrLF &_ 
    CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"  
    WScript.Quit 
Else 
    Do 
     Pause(10) ' Pause 10 seconds 
     If CheckProcess(DblQuote(ProcessPath)) = False Then 
      Call Logoff() 
     End If 
    Loop 
End If 
'************************************************************************** 
Function CheckProcess(ProcessPath) 
    Dim strComputer,objWMIService,colProcesses,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 
     CheckProcess = False 
    Else 
     CheckProcess = True 
    End if 
End Function 
'************************************************************************** 
Function DblQuote(Str) 
    DblQuote = Chr(34) & Str & Chr(34) 
End Function 
'************************************************************************** 
Sub Logoff() 
    Dim ws,Command,Execution 
    Set ws = CreateObject("wscript.Shell") 
    Command = "Cmd /c shutdown.exe -L -F" 
    Execution = ws.run(Command,0,True) 
End sub 
'************************************************************************** 
Sub Pause(Secs)  
    Wscript.Sleep(Secs * 1000)  
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  
'*************************************************************************** 
Function CommandLineLike(ProcessPath) 
    ProcessPath = Replace(ProcessPath, "\", "\\") 
    CommandLineLike = "'%" & ProcessPath & "%'" 
End Function 
'**************************************************************************** 
+1

謝謝那訣竅:) –