In a vbscript, how can i get the process id of the

2019-05-30 05:15发布

问题:

within a vb script, I want to assign a variable with the process id of the cmd.exe in which the vb script is running. Is there any command?

回答1:

Below is the example VB script procedure returning parent process caption and id:

GetParentProcessInfo sCaption, sProcessId

MsgBox "Parent Process Caption '" & sCaption & "'" & vbCrLf & "Parent Process Id '" & sProcessId & "'"

Sub GetParentProcessInfo(sCaption, sProcessId)
    With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & CreateObject("WScript.Shell").Exec("rundll32 kernel32,Sleep").ProcessId & "'")
        With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
            With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
                sCaption = .Caption
                sProcessId = .ProcessId
            End With
        End With
        .Terminate
    End With
End Sub