Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBS Getting its Process ID

1,964 views
Skip to first unread message

Bryan McCauley

unread,
Jul 15, 2003, 1:55:32 PM7/15/03
to
I know I must be missing it in the documentation, but I
can't find it. Is there are way for a VBS script to
determine its process ID?

There may be multiple wscript.exe running, so I can't
just look for wscript.exe in the process table. If I was
running XP, I am running NT, I could use the CommandLine
Parameter.

Thanks for any help you could provide,

Bryan

Joe Earnest

unread,
Jul 15, 2003, 2:29:42 PM7/15/03
to
Hi,

"Bryan McCauley" <bryan_m...@reyrey.com> wrote in message
news:023e01c34afa$495e44a0$a301...@phx.gbl...


> I know I must be missing it in the documentation, but I
> can't find it. Is there are way for a VBS script to
> determine its process ID?
>
> There may be multiple wscript.exe running, so I can't
> just look for wscript.exe in the process table. If I was
> running XP, I am running NT, I could use the CommandLine
> Parameter.

A list of alternatives:

Launch the subject script from a generic script using WshShell.Exec or the
WMI process object to get an immediate return. E.g.:

' WshShell.Exec method
set oShell= createObject("wscript.shell")
set oExec= oShell.exec(sCmdLine)
nProcessId= oExec.processId

' WMI per ByRef variable return (version 1)
set oProcess= getobject("winmgmts:!Win32_Process")
nErrRtn= oProcess.create(sCmdLine,sActiveDir,, nProcessId)

' WMI per ByRef variable return (version 2)
sCptrName= "." ' for a local computer
set oWMI= getObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& sCptrName & "\root\cimv2")<br />
set oProcess= oWMI.get("Win32_Process")
nErrRtn= oProcess.create(sCmdLine,sActiveDir,, nProcessId)

Since you already seem to be getting a list of running processes, consider
copying and renaming wscript.exe to something like "specialprocess1.exe".
Then run it as "specialprocess1.exe scriptpathname.vbs". This will enable
you to find it in the list of running processes.

Get a third party utility such as TList.exe, that will give you both the
PIDs and filenames for all wscript processes. (Sorry, don't have the url
handy -- just Google search it. BTW, I haven't used this utility, but am
just passing along prior recommendations.)

Joe Earnest

Joe Earnest

unread,
Jul 15, 2003, 2:34:07 PM7/15/03
to
Hi,

Sorry, I overlooked that you were running NT. As you note, some of these
alternatives may not be available to you.

Joe Earnest

Bryan McCauley

unread,
Jul 31, 2003, 8:35:20 AM7/31/03
to
Thanks for the input, what I finaly ended up doing was
executing CMD.EXE via WshShell.Exec which returned an
WshScriptExec object. One of the Properties of the
WshScriptExec object is the ProccesID of the command. I
took that PID and went through WMI until I found the
matching PID. Once I had the correct WMI object I read
the ParentProcessID property, which gave me the
information I was looking for. Finally I terminated the
CMD.exe via the WshScriptExe.Terminate method.

>.
>

Joe Earnest

unread,
Jul 31, 2003, 1:18:07 PM7/31/03
to
Hi,

"Bryan McCauley" <bryan_m...@reyrey.com> wrote in message

news:599801c35760$345ccdc0$a001...@phx.gbl...


> Thanks for the input, what I finaly ended up doing was
> executing CMD.EXE via WshShell.Exec which returned an
> WshScriptExec object. One of the Properties of the
> WshScriptExec object is the ProccesID of the command. I
> took that PID and went through WMI until I found the
> matching PID. Once I had the correct WMI object I read
> the ParentProcessID property, which gave me the
> information I was looking for. Finally I terminated the
> CMD.exe via the WshScriptExe.Terminate method.

That's a vaery nice workaround (if somewaht laborious) ;-). Thanks for
posting back. If you don't mind posting the excerpted code someone else
(like me) might find it useful to tuck away for future reference.

Regards,
Joe Earnest

0 new messages