Find my own process ID in VBScript

后端 未结 10 1001
小蘑菇
小蘑菇 2020-11-28 14:49

I\'m using the following code snippet to determine what process ID my vbscript is running as:

On Error Resume Next
Dim iMyPID : iMyPID = GetObject(\"winmgmts         


        
相关标签:
10条回答
  • 2020-11-28 15:13

    Get the current processID

    Set WshShell = CreateObject("WScript.Shell")
    currentProgram=wscript.ScriptName
    Const strComputer = "."
    Dim objWMIService, colProcessList
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    query="SELECT * FROM Win32_Process WHERE Name = 'wscript.exe' "  
    Set colProcessList = objWMIService.ExecQuery(query)
    For Each objProcess in colProcessList 
    
    If (InStr (objProcess.commandLine,wscript.ScriptName) <> 0 )Then
     processDetails="Current ProcessId : "& objProcess.ProcessId & " \n, And Process Name:" & objProcess.name &"\n CommandLine is :"& objProcess.CommandLine
     message = msgbox(processDetails,16,"Details")
    End If
    
    0 讨论(0)
  • 2020-11-28 15:14

    Here's an even better code snippet:

          ' ***********************************************************************************************************
          ' lng_MyProcessID finds and returns my own process ID. This is excruciatingly difficult in VBScript. The
          ' method used here forks "cmd /c pause" with .Exec, and then uses the returned .Exec object's .ProcessID 
          ' attribute to feed into WMI to get that process's Win32_Process descriptor object, and then uses THAT
          ' WMI Win32_Process descriptor object's .ParentProcessId attribute, which will be OUR Process ID, and finally
          ' we terminate the waiting cmd process. Execing cmd is what causes the brief cmd window to flash at start up,
          ' and I can' figure out out how to hide that window.
    
          ' returns: My own Process ID as a long int; zero if we can't get it.
          ' ************************************************************************************************************
    
          Function lng_MyProcessID ()
    
            lng_MyProcessID = 0                     ' Initially assume failure
    
            If objWMIService Is Nothing Then Exit Function      ' Should only happen if in Guest or other super-limited account
    
            Set objChildProcess = objWshShell.Exec ( """%ComSpec%"" /C pause" ) ' Fork a child process that just waits until its killed
    
            Set colPIDs= objWMIService.ExecQuery ( "Select * From Win32_Process Where ProcessId=" & objChildProcess.ProcessID,, 0 )
    
            For Each objPID In colPIDs                  ' There's exactly 1 item, but .ItemIndex(0) doesn't work in XP
    
              lng_MyProcessID = objPID.ParentProcessId          ' Return child's parent Process ID, which is MY process ID!
    
            Next
    
            Call objChildProcess.Terminate()                ' Terminate our temp child
    
          End Function ' lng_MyProcessID
    
    0 讨论(0)
  • 2020-11-28 15:20

    This is not my answer, I found this in some google groups discussion forum... See if it helps you.

    Set objSWbemServices = GetObject ("WinMgmts:Root\Cimv2")
    Set colProcess = objSWbemServices.ExecQuery ("Select * From Win32_Process")
    
    For Each objProcess In colProcess
        If InStr (objProcess.CommandLine, WScript.ScriptName) <> 0 Then
          WScript.Echo objProcess.Name, objProcess.ProcessId, objProcess.CommandLine
        End If
    Next
    

    Original Discussion Thread in Google Groups forum

    0 讨论(0)
  • 2020-11-28 15:23

    To retrieve the own process ID of a VB Script you can rely on the property CreationDate of the Process object.

    At the moment a VB Script is started, the process that runs the script will have the latest CreationDate of all processes that runs the same script.

    In fact, it will have the highest CreationDate of all running processes.

    So, to get the PID, first thing to do is to search for the process with the highest CreationDate.

    'Searching for processes
    Dim strScriptName
    Dim WMI, wql
    Dim objProcess
    '
    'My process
    Dim datHighest
    Dim lngMyProcessId
    
    
    'Which script to look for ? 
    strScriptName = "WScript.exe"
    'strScriptName = "Notepad.exe"
    
    'Iniitialise 
    datHighest = Cdbl(0)
    
    Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    wql = "SELECT * FROM Win32_Process WHERE Name = '" & strScriptName & "'"
    '
    For Each objProcess In WMI.ExecQuery(wql)
      'The next If is not necessary, it only restricts the search to all processes on the current VB Script
      'If Instr(objProcess.CommandLine, WScript.ScriptName) <> 0 Then
        If objProcess.CreationDate > datHighest Then
          'Take the process with the highest CreationDate so far
          '  e.g. 20160406121130.510941+120   i.e. 2016-04-06 12h11m:30s and fraction
          datHighest = objProcess.CreationDate
          lngMyProcessId = objProcess.ProcessId
        End If
      'End If
    Next
    
    'Show The result
    WScript.Echo "My process Id = " & lngMyProcessId
    
    0 讨论(0)
提交回复
热议问题