【问题标题】:Batch Get specific PID批量获取特定PID
【发布时间】:2015-08-13 09:21:13
【问题描述】:

我正在尝试从 Batch 中杀死一个 vbscript,到目前为止我得到了这个:

for /f "tokens=2" %%a in ('tasklist^|find /i "wscript.exe"') do (set pid=%%a)

这得到 a wscript PID,所以我猜如果有多个正在运行的 wscript,它只会随机取一个?我真的不知道。但我想获得 specific 进程/任务的 PID,确切地说是这个: http://i.imgur.com/hsNK5IO.png 有没有办法从命令行获取 PID?

【问题讨论】:

    标签: batch-file vbscript pid


    【解决方案1】:

    你可以试试这个 vbscript:Wscript_Killer_Selector.vbs

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

    【讨论】:

      【解决方案2】:

      wmic可以通过命令行过滤进程:

      @echo off
      
      
      for /f  "tokens=* delims=" %%a in ('
          wmic process where "name='wscript.exe' and commandline like '%%5MinutesMSG%%'" get ProcessID /format:value
      ') do (
          for /f  "tokens=* delims=" %%# in ("%%a") do (
              set "%%#"
          )
      )
      
      echo %processid%
      

      【讨论】:

      • 工作得很好。谢谢!添加一个 taskkill /f /pid %processid% 节省了我的一天 - 谢谢! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2014-11-11
      • 2011-09-13
      • 2023-03-08
      • 2013-03-05
      相关资源
      最近更新 更多