【问题标题】:Get a process path with VBS使用 VBS 获取进程路径
【发布时间】:2018-09-14 01:53:46
【问题描述】:

我想用 vbs 杀死一个进程,我知道命令是:

oShell.Run "taskkill /im software.exe", , True

在我们杀死它之前如何获取software.exe路径?

因为我想再次启动software.exe。

【问题讨论】:

    标签: vbscript process


    【解决方案1】:

    WMI(也可以终止):

    dim wmi, list, process, path, shell
    
    set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
    set list = wmi.ExecQuery("Select * from Win32_Process") 
                      '// or "Select * from Win32_Process where name = 'xxxxxxx.exe'" allowing the removal of the if block
    
    for each process in list
        if (lcase(process.name) = "xxxxxxx.exe") then
            path = process.ExecutablePath
            process.terminate()
            exit for
        end if
    next
    
    wscript.echo path
    
    set shell = CreateObject("WScript.Shell")
    shell.Run Chr(34) & path & Chr(34)
    

    【讨论】:

    • 好的,谢谢它的工作,我只是添加: Set oShell = WScript.CreateObject("WScript.Shell")
    【解决方案2】:

    试试这个 vbscript:

    Option Explicit
    Dim Titre,Copyright,fso,ws,NomFichierLog,temp,PathNomFichierLog,OutPut,Count,strComputer
    If AppPrevInstance() Then 
        MsgBox "Il y a une instance déjà en cours" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"Il y a une instance déjà en cours"    
        WScript.Quit   
    Else 
    Copyright = "[© Hackoo © 2015 ]"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ws = CreateObject( "Wscript.Shell" )
    NomFichierLog="Killed Process.txt"
    temp = ws.ExpandEnvironmentStrings("%temp%")
    PathNomFichierLog = temp & "\" & NomFichierLog
    Set OutPut = fso.CreateTextFile(temp & "\" & NomFichierLog,1)
    strComputer = "."
        Call Find("wscript.exe")
        Call Explorer(PathNomFichierLog)
    End If
    '***************************************************************************************************
    Function Explorer(File)
        Dim ws
        Set ws = CreateObject("wscript.shell")
        ws.run "Explorer "& File & "\",1,True
    end Function
    '***************************************************************************************************
    Sub Find(MyProcess)
        Dim colItems,objItem,Processus,Question
        Titre = " Processus "& DblQuote(MyProcess) &" en cours d'exécution "
        Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
        & "Where Name like '%"& MyProcess &"%' AND NOT commandline like " & CommandLineLike(WScript.ScriptFullName) & "",,48)
        Count = 0 
        For Each objItem in colItems
            Count= Count + 1
            'Processus = Mid(objItem.CommandLine,InStr(objItem.CommandLine,""" """) + 2) 'Extraction du chemin du script en ligne de commande
            Processus = objItem.CommandLine 'Replace(Processus,chr(34),"")
            Question = MsgBox ("Voulez-vous arrêter ce script : " & DblQuote(Processus) & " ?" ,VBYesNO+VbQuestion,Titre+Copyright)
            If Question = VbYes then
                objItem.Terminate(0)'Tuer ce processus
                OutPut.WriteLine Processus
            else
                Count= Count - 1 'décrementer le compteur de -1
            End if
        Next
    OutPut.WriteLine String(100,"*")
    OutPut.WriteLine count & Titre & "ont été arrêtés"
    OutPut.WriteLine String(100,"*") & VbCrLF 
    End Sub
    '**************************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '**************************************************************************
    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
    '**************************************************************************
    

    或者这个:

    Option Explicit
    Dim Ws,fso,LogFile,Command,Execution
    Set Ws = CreateObject("Wscript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "log"
    Call Kill("calc.exe")
    ws.run LogFile
    '****************************************************************************************************
    Sub Kill(Process)
            Command = "Taskkill /F /IM "&Process&" > LogTmp.txt & Cmd /U /C Type LogTmp.txt > "&LogFile&" & Del LogTmp.txt"
            Call Executer(Command,0)
    End Sub
    '****************************************************************************************************
    Function Executer(StrCmd,Console)
        Dim ws,MyCmd,Resultat
        Set ws = CreateObject("wscript.Shell")
    'La valeur 0 pour cacher la console MS-DOS
        If Console = 0 Then
            MyCmd = "CMD /C " & StrCmd & ""
            Resultat = ws.run(MyCmd,Console,True)
            If Resultat = 0 Then
            Else
                MsgBox "Une erreur inconnue est survenue !",16,"Une erreur inconnue est survenue !"
            End If
        End If
    'La valeur 1 pour montrer la console MS-DOS
        If Console = 1 Then
            MyCmd = "CMD /K " & StrCmd & ""
            Resultat = ws.run(MyCmd,Console,False)
            If Resultat = 0 Then
            Else
                MsgBox "Une erreur inconnue est survenue !",16,"Une erreur inconnue est survenue !"
            End If
        End If
        Executer = Resultat
    End Function
    '****************************************************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '****************************************************************************************************
    

    【讨论】:

      【解决方案3】:

      使用 VBScript 重新启动应用程序(关闭并再次打开)

      使用 Win32_ProcessCommandLine 属性更准确地获取应用程序路径。

      如果您不记得或不知道任务管理器中的确切应用程序名称,请使用 InStr

      'open google chrome explorer before you run this script
      On Error Resume next
      Dim processes, process, aPath, sh
      Set processes=GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process") 
      
       For Each  process In processes
          If (InStr(1,process.CommandLine,"chrom",1)) > 0 Then ' "chrom" some letter form "chrome.exe"
               aPath = process.CommandLine
               process.Terminate()
               Exit For 
          End If 
       Next 
      
          Set sh = CreateObject("WScript.Shell")
          sh.Popup "App Path : "&aPath,2,"In_The_Name_Of_Allah"
          sh.exec aPath
      

      您可以删除 "chrom" 并添加任何应用程序名称或名称中的某些字母以将其关闭并重新打开。

      如果您想从多实例打开中关闭并重新打开特定实例,这几段代码可以发挥作用

      例如,您使用 notepad.exe 打开了多个文本文件,并且您想关闭并仅重新打开一个特定文本而让其他文本保持打开状态,只需删除 "chrom" 并添加您想要关闭并重新打开的此文本的名称。
      对于使用 wscript.exe 运行的多脚本也是如此,并且您希望关闭特定的一个脚本而不是所有脚本并重新打开,您可以使用相同的类型输入此脚本的名称,而不是 "chrom"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多