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?
2 Answers
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 4 Comments
bharathi priya T
Thanks. I am new vb script. I have to write a vbs that kills all the cmd.exe except the one in which the vb script is going to run. Below is the code which I was trying.
bharathi priya T
Set WshShell = WScript.CreateObject("WScript.Shell") Set a = WshShell.Exec(cmd /c "wmic process get parentprocessid,name|find WMIC") x = a.StdOut.ReadLine Wscript.Echo x pid = Right(x,4) Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'cmd.exe' and ProcessID != &abc") For Each item In colProcesses item.terminate() Next
omegastripes
@bharathipriyaT please don't post this as the comments, but add all these details into your question by editing it. Describe how does your code works, what's wrong and what is the expected behavior. Then I may suggest some code.
David Gausmann
This solution has the advantage that it doesn't use a console program. In former times I experienced big trouble when to many console programs were executed on a disconnected terminal server session (they had all been properly terminated). rundll32 instead is a regular windows application (no console window shown), so this error will not appear.