vbscript 如何查看程序是否打开然后关闭它 [英] vbscript how can you see if a program is open then close it

查看:34
本文介绍了vbscript 如何查看程序是否打开然后关闭它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 vbscript 确定程序是否正在运行或打开,如果是,则关闭其他程序?

How can I use vbscript to determine if a program is running or open and, if it is, close a different program?

例如,我如何检查 game.exe 是否正在运行,如果是,则关闭 google.exe?

For example, how could I check to see if game.exe is running and, if it is, then close google.exe?

推荐答案

这里有一个示例,向您展示如何按名称检查进程,如果检查结果为正则将其终止.

Here is an example that show you how to check a process by name and if the check is positive so it kill it.

例如,我选择游戏为 Calc.exe,如果脚本找到它,它将被杀死,也适用于 internet explorerGoogle Chrome

For example i choose the Game as Calc.exe and if the script found it, it will be killed, and also for internet explorer and Google Chrome

Option Explicit
If AppPrevInstance() Then   
    MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else   
Dim Game,Browser1,Browser2
Game = "%windir%\system32\calc.exe"
Browser1 = "%ProgramFiles%\Google\Chrome\Application\chrome.exe"
Browser2 = "%ProgramFiles%\Internet Explorer\iexplore.exe"
    Do   
        Call Main(Array(Game,Browser1,Browser2))
        Call Pause(1) 'Sleeping for 1 minute
    Loop   
End If   
'**************************************************************************
Sub Main(colProcessPaths)   
    Dim ProcessPath   
    For Each ProcessPath In colProcessPaths     
        CheckProcess(ProcessPath)   
    Next   
End Sub   
'**************************************************************************
Sub CheckProcess(ProcessPath)   
    On error resume Next
    Dim Process,objWMIService,colProcesses,wshShell,btn,Timeout,User
    Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Process WHERE Commandline LIKE " & CommandLineLike(ProcessName))
    For Each Process in colProcesses    
        If colProcesses.Count > 0 Then
            Set wshShell = CreateObject("WScript.Shell")
            User = CreateObject("WScript.Network").UserName
            Timeout = 30 'Call the Popup method with a 30 seconds timeout.
            btn = WshShell.Popup("Hello "& DblQuote(User) & " !" & vbcr &_
            "Your Administrator has requested you to log out of this application after work. " & vbcr &_
            "We have detected you are still using the program : "& DblQuote(ProcessName) & vbcr &_
            "Please press on cancel button if you are still at your machine ?",Timeout,"Question", vbOKCancel + vbQuestion)
            Select Case btn
' Yes button pressed.
            case 1
                Process.Terminate(0)
' No button pressed.
            case 2
                Exit Sub
' Timed out.
            case -1
                Process.Terminate(0)
            End Select
        Else    
            Exit Sub    
        End if 
    Next            
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
'**************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

这篇关于vbscript 如何查看程序是否打开然后关闭它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆