Hello,

I need to know the current ID process of a running script.
In other words, while a script is being executed, I need to get the ID
process of the script itself.

Thx in advance for any help.

Alfredo

Re: Current Process by Joe

Joe
Sun Feb 08 18:03:49 CST 2004

Hi,

"Alfredo Ardito" <alfredo.ardito@libero.it> wrote in message
news:#zbuFgl7DHA.3420@TK2MSFTNGP11.phx.gbl...
| Hello,
|
| I need to know the current ID process of a running script.
| In other words, while a script is being executed, I need to get the ID
| process of the script itself.
|
| Thx in advance for any help.

I need to clarify what you're after and the context, because the techniques
are different: (1) Do you want to get a running VBS script to get its own
PID?, or (2) do you want a running VBS script to get the PID of a secondary
script that it runs? In either case, are you running locally or remotely
(i.e., can you use the Exec method)?

Joe Earnest



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04



Re: Current Process by Alfredo

Alfredo
Mon Feb 09 03:42:39 CST 2004

Hi,

It's the 1st case i'm interested in. I'm running a VBS script and I need to
get its own PID from inside the script itself.

Thx for your attention.

Regards
Alfredo

"Joe Earnest" <joeearnestNO@SPAMqwest.netPLEASE> wrote in message
news:%23UDfH8p7DHA.2644@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> "Alfredo Ardito" <alfredo.ardito@libero.it> wrote in message
> news:#zbuFgl7DHA.3420@TK2MSFTNGP11.phx.gbl...
> | Hello,
> |
> | I need to know the current ID process of a running script.
> | In other words, while a script is being executed, I need to get the ID
> | process of the script itself.
> |
> | Thx in advance for any help.
>
> I need to clarify what you're after and the context, because the
techniques
> are different: (1) Do you want to get a running VBS script to get its own
> PID?, or (2) do you want a running VBS script to get the PID of a
secondary
> script that it runs? In either case, are you running locally or remotely
> (i.e., can you use the Exec method)?
>
> Joe Earnest
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04
>
>




Re: Current Process by Alfredo

Alfredo
Mon Feb 09 03:24:27 CST 2004

Hi,

It's the 1st case i'm interested in. I'm running a VBS script and I need to
get its own PID from inside the script itself.

Thx for your attention.

Regards
Alfredo

"Joe Earnest" <joeearnestNO@SPAMqwest.netPLEASE> wrote in message
news:%23UDfH8p7DHA.2644@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> "Alfredo Ardito" <alfredo.ardito@libero.it> wrote in message
> news:#zbuFgl7DHA.3420@TK2MSFTNGP11.phx.gbl...
> | Hello,
> |
> | I need to know the current ID process of a running script.
> | In other words, while a script is being executed, I need to get the ID
> | process of the script itself.
> |
> | Thx in advance for any help.
>
> I need to clarify what you're after and the context, because the
techniques
> are different: (1) Do you want to get a running VBS script to get its own
> PID?, or (2) do you want a running VBS script to get the PID of a
secondary
> script that it runs? In either case, are you running locally or remotely
> (i.e., can you use the Exec method)?
>
> Joe Earnest
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04
>
>



Re: Current Process by Joe

Joe
Mon Feb 09 09:03:02 CST 2004

Hi,

"Alfredo Ardito" <alfredo.ardito@libero.it> wrote in message
news:OIFKSEv7DHA.696@tk2msftngp13.phx.gbl...
| Hi,
|
| It's the 1st case i'm interested in. I'm running a VBS script and I need
to
| get its own PID from inside the script itself.
|
| Thx for your attention.

Two ways, but you need WMI for both.

(1) If WSH-hosted, the first is to just loop through the WMI Process data
for the last instance of "wscript.exe". This is not theoretically certain,
if you're in an environment where scripts fire regularly, but it seems to
work in practice for most people who have posted. It should be the first
thing the script does. Use something like the following:

sCmpName= "." 'local computer

for each oProc in getObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" _
& sCmpName & "\root\cimv2" _
).instancesOf("Win32_Process")
if (lcase(oProc.name)="wscript.exe") then
nPID= oProc.processId
end if
next

(2) If running locally, to be certain, run an innocuous utility like cmd.exe
or, my preference, have the script write a temporary vbs file that does
nothing but sleep for a few seconds. ("WScript.Sleep 8000") Use Exec to
run it, get its PID from Exec, loop through the WMI process data for the PID
and get its parent PID (the main script's PID), then terminate the secondary
process if necessary (not necessary with a script that only sleeps for a few
seconds). Use something like the following:

' write "wscript.sleep 8000" to TmpSleep.vbs

set oWshShell= createobject("wscript.shell")
set oExec= oWshShell.exec( "wscript " _
& """<path>\TmpSleep.vbs""")
nTmpVbsPID= clng(oExec.processId)

sCmpName= "." 'local computer

for each oProc in getObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" _
& sCmpName & "\root\cimv2" _
).instancesOf("Win32_Process")
if (clng(oProc.processId))=nTmpVbsPID) then
nPID= oProc.parentProcessId
exit for
end if
next

oExec.terminate 'for non-self terminating test utilities



Joe Earnest



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04



Re: Current Process by Torgeir

Torgeir
Mon Feb 09 16:21:46 CST 2004

Joe Earnest wrote:

> Two ways, but you need WMI for both.
>
> (1) If WSH-hosted, the first is to just loop through the WMI Process data
> for the last instance of "wscript.exe". This is not theoretically certain,
> if you're in an environment where scripts fire regularly, but it seems to
> work in practice for most people who have posted. It should be the first
> thing the script does. Use something like the following:
> (snip)
>
> (2) If running locally, to be certain, run an innocuous utility like cmd.exe
> or, my preference, have the script write a temporary vbs file that does
> nothing but sleep for a few seconds. ("WScript.Sleep 8000") Use Exec to
> run it, get its PID from Exec, loop through the WMI process data for the PID
> and get its parent PID (the main script's PID), then terminate the secondary
> process if necessary (not necessary with a script that only sleeps for a few
> seconds). Use something like the following:
> (snip)

Two more alternatives:

(3)
Run the script with a copy of Wscript.exe (or Cscript.exe), where the
copy has a unique name you can test for in the process list (this will
only be reliable if you never will run more than one instance of your
script simultaneously).

An example:

Copy Wscript.exe (or Wscript.exe) to MyWscript.exe and run the script below with
the following command:

MyWscript.exe "path to vbs file"


sCmpName= "." 'local computer

for each oProc in getObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" _
& sCmpName & "\root\cimv2" _
).instancesOf("Win32_Process")
if lcase(oProc.name) = "mywscript.exe" then
nPID = oProc.processId
end if
next

wscript.echo "My PID is : " & nPID


(4)
With WinXP, you can use WMI to access the command line of each process,
so you can check which wscript/cscript task is running a script with
the current script name (again, this will only be reliable if you never
will run more than one instance of your script simultaneously).

Example:

sCmpName= "." 'local computer
sScriptName = wscript.scriptName

for each oProc in getObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" _
& sCmpName & "\root\cimv2" _
).instancesOf("Win32_Process")
if lcase(oProc.name) = "wscript.exe" _
or lcase(oProc.name) = "cscript.exe" Then
sCmdLine = oProc.commandLine
if instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0 then
nPID = oProc.processId
end if
end if
next

wscript.echo "My PID is : " & nPID



--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Re: Current Process by Joe

Joe
Mon Feb 09 19:54:05 CST 2004

Hi Torgeir,

"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:402807FA.21C5966F@hydro.com...
| Two more alternatives:
[snip]
| (4)
| With WinXP, you can use WMI to access the command line of each process,
| so you can check which wscript/cscript task is running a script with
| the current script name (again, this will only be reliable if you never
| will run more than one instance of your script simultaneously).
|
| Example:
|
| sCmpName= "." 'local computer
| sScriptName = wscript.scriptName
|
| for each oProc in getObject( _
| "winmgmts:{impersonationLevel=impersonate}!\\" _
| & sCmpName & "\root\cimv2" _
| ).instancesOf("Win32_Process")
| if lcase(oProc.name) = "wscript.exe" _
| or lcase(oProc.name) = "cscript.exe" Then
| sCmdLine = oProc.commandLine
| if instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
| or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
| or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0 then
| nPID = oProc.processId
| end if
| end if
| next
|
| wscript.echo "My PID is : " & nPID

Thanks. I was not aware of this last method. Is the point of concatenating
the three character prefixes and testing separately, instead of just testing
for InStr(sCmdLine, sScriptName), simply to avoid inclusive script names, or
is there another reason? Also does this work if the script is run through a
context-menu shell verb, the run box or a shortcut that doesn't include
wscript.exe or cscript.exe (i.e., does WinXp create the command line for
non-command-line situations)?

Regards,
Joe Earnest



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04



Re: Current Process by Torgeir

Torgeir
Mon Feb 09 20:50:14 CST 2004

Joe Earnest wrote:

> Torgeir Bakken (MVP) wrote:
> (snip)
> > sCmdLine = oProc.commandLine
> > if instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
> > or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
> > or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0 then
> > nPID = oProc.processId
> > end if
>
> Thanks. I was not aware of this last method. Is the point of concatenating
> the three character prefixes and testing separately, instead of just testing
> for InStr(sCmdLine, sScriptName), simply to avoid inclusive script names, or
> is there another reason?

Yes, it is to avoid inclusive script names, and depending on how you launch
your script, the script file name input can have several forms, e.g.:

cscript.exe c:\scripts\tst.vbs
cscript.exe tst.vbs
cscript.exe "tst.vbs"

The If statement in the quoted code above tests for these three types,
but it can fail if other scripts are running with names that also will
be included in the If test. E.g. the example above would not handle
another script with the name "my tst.vbs" very well ;-)

I see now that I should first have tried to match the command line with
the WScript.ScriptFullName, so if wscript/cscript is launched with the
full path to the script, I would always get a correct hit (unless several
instances of the same script file is run in parallel).

Here is a better version:

sCmpName= "." 'local computer

sScriptFullName = wscript.scriptFullName
sScriptName = wscript.scriptName

for each oProc in getObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" _
& sCmpName & "\root\cimv2" _
).instancesOf("Win32_Process")
if lcase(oProc.name) = "wscript.exe" _
or lcase(oProc.name) = "cscript.exe" Then
sCmdLine = oProc.commandLine
if instr(1, sCmdLine, sScriptFullName, vbTextCompare) > 0 then
nPID = oProc.processId
exit for ' a certain hit, exit the loop
elseif instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0 then
nPID = oProc.processId
' omitting "exit for" here in case of inclusive script and
' that scriptFullName in that case gives a more correct
' "hit" later on in the loop.
end if
end if
next

wscript.echo "My PID is : " & nPID


> Also does this work if the script is run through a
> context-menu shell verb, the run box or a shortcut that doesn't include
> wscript.exe or cscript.exe (i.e., does WinXp create the command line for
> non-command-line situations)?

All processes in your computer has a command line property regardless of
how they are launched. For wscript/cscript, the format for the file input
parameter will be different depending on how you launch your vbscript,
but it will always contain the file name at least.

Sadly enough, it was first in WinXP that the CommandLine property was exposed
through WMI.

For e.g. Win2k, if you want to get to the command line property for a process,
you can use tlist.exe in the Win2k "Support Tools". If you run "tlist.exe
<pid>", it will be listed for the process with that PID.

E.g. here is the output for AutoItX's help file process running on my computer
just now:

C:\>tlist 2428
2428 hh.exe AutoItX Help
CWD: F:\wsh\AutoItX\AutoItX\
CmdLine: "C:\WINNT\hh.exe" F:\wsh\AutoItX\AutoItX\AutoItX.chm
(snip)


For a GUI version, I prefer Process Explorer (free) from
http://www.sysinternals.com , just right click on a process and
select Properties to see what command line it is started up with.



--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Re: Current Process by Joe

Joe
Tue Feb 10 09:04:52 CST 2004

Hi,

Cool. Thanks for the wealth of info.

Regards,
Joe Earnest

"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:402846E6.5F4A45D0@hydro.com...
| Joe Earnest wrote:
|
| > Torgeir Bakken (MVP) wrote:
| > (snip)
| > > sCmdLine = oProc.commandLine
| > > if instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
| > > or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
| > > or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0 then
| > > nPID = oProc.processId
| > > end if
| >
| > Thanks. I was not aware of this last method. Is the point of
concatenating
| > the three character prefixes and testing separately, instead of just
testing
| > for InStr(sCmdLine, sScriptName), simply to avoid inclusive script
names, or
| > is there another reason?
|
| Yes, it is to avoid inclusive script names, and depending on how you
launch
| your script, the script file name input can have several forms, e.g.:
|
| cscript.exe c:\scripts\tst.vbs
| cscript.exe tst.vbs
| cscript.exe "tst.vbs"
|
| The If statement in the quoted code above tests for these three types,
| but it can fail if other scripts are running with names that also will
| be included in the If test. E.g. the example above would not handle
| another script with the name "my tst.vbs" very well ;-)
|
| I see now that I should first have tried to match the command line with
| the WScript.ScriptFullName, so if wscript/cscript is launched with the
| full path to the script, I would always get a correct hit (unless several
| instances of the same script file is run in parallel).
|
| Here is a better version:
|
| sCmpName= "." 'local computer
|
| sScriptFullName = wscript.scriptFullName
| sScriptName = wscript.scriptName
|
| for each oProc in getObject( _
| "winmgmts:{impersonationLevel=impersonate}!\\" _
| & sCmpName & "\root\cimv2" _
| ).instancesOf("Win32_Process")
| if lcase(oProc.name) = "wscript.exe" _
| or lcase(oProc.name) = "cscript.exe" Then
| sCmdLine = oProc.commandLine
| if instr(1, sCmdLine, sScriptFullName, vbTextCompare) > 0 then
| nPID = oProc.processId
| exit for ' a certain hit, exit the loop
| elseif instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
| or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
| or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0
then
| nPID = oProc.processId
| ' omitting "exit for" here in case of inclusive script and
| ' that scriptFullName in that case gives a more correct
| ' "hit" later on in the loop.
| end if
| end if
| next
|
| wscript.echo "My PID is : " & nPID
|
|
| > Also does this work if the script is run through a
| > context-menu shell verb, the run box or a shortcut that doesn't include
| > wscript.exe or cscript.exe (i.e., does WinXp create the command line for
| > non-command-line situations)?
|
| All processes in your computer has a command line property regardless of
| how they are launched. For wscript/cscript, the format for the file input
| parameter will be different depending on how you launch your vbscript,
| but it will always contain the file name at least.
|
| Sadly enough, it was first in WinXP that the CommandLine property was
exposed
| through WMI.
|
| For e.g. Win2k, if you want to get to the command line property for a
process,
| you can use tlist.exe in the Win2k "Support Tools". If you run "tlist.exe
| <pid>", it will be listed for the process with that PID.
|
| E.g. here is the output for AutoItX's help file process running on my
computer
| just now:
|
| C:\>tlist 2428
| 2428 hh.exe AutoItX Help
| CWD: F:\wsh\AutoItX\AutoItX\
| CmdLine: "C:\WINNT\hh.exe" F:\wsh\AutoItX\AutoItX\AutoItX.chm
| (snip)
|
|
| For a GUI version, I prefer Process Explorer (free) from
| http://www.sysinternals.com , just right click on a process and
| select Properties to see what command line it is started up with.
|
|
|
| --
| torgeir
| Microsoft MVP Scripting and WMI, Porsgrunn Norway
| Administration scripting examples and an ONLINE version of the 1328 page
| Scripting Guide: http://www.microsoft.com/technet/scriptcenter
|
|


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04



Re: Current Process by Joe

Joe
Tue Feb 10 12:13:57 CST 2004

Hi Torgeir,

[snipped]

"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:402846E6.5F4A45D0@hydro.com...
...
| I see now that I should first have tried to match the command line with
| the WScript.ScriptFullName, so if wscript/cscript is launched with the
| full path to the script, I would always get a correct hit (unless several
| instances of the same script file is run in parallel).
|
| Here is a better version:
|
| sCmpName= "." 'local computer
|
| sScriptFullName = wscript.scriptFullName
| sScriptName = wscript.scriptName
|
| for each oProc in getObject( _
| "winmgmts:{impersonationLevel=impersonate}!\\" _
| & sCmpName & "\root\cimv2" _
| ).instancesOf("Win32_Process")
| if lcase(oProc.name) = "wscript.exe" _
| or lcase(oProc.name) = "cscript.exe" Then
| sCmdLine = oProc.commandLine
| if instr(1, sCmdLine, sScriptFullName, vbTextCompare) > 0 then
| nPID = oProc.processId
| exit for ' a certain hit, exit the loop
| elseif instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
| or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
| or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0
then
| nPID = oProc.processId
| ' omitting "exit for" here in case of inclusive script and
| ' that scriptFullName in that case gives a more correct
| ' "hit" later on in the loop.
| end if
| end if
| next
|
| wscript.echo "My PID is : " & nPID

In incorporating this into my library routines, I came up with a method that
may give you a bit more certainty, without running the full script pathname
comparison first. It tests for the three path-related invalid file
characters (" : \) preceding the script file name. If so, it's a match; if
not, it then left trims the command line of white space and tests whether
the file name is the initial entry on the command line. The script
continues to loop, so it should find and return the latest running instance
of multiple copies. It could still be confused by an ornately complex
command line or truly fast firing multiple copies of the script, but it
should be reasonably solid.

---
sScriptName = lcase(wscript.scriptName)
sCptrName= "." 'local
set oWmiCim= getObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" _
& sCptrName & "\root\cimv2")

for each oProc in _
oWmiCim.instancesOf("Win32_Process")
with oProc
if (lcase(.name)="wscript.exe") then
sCmdLine= lcase(.commandLine)
pCmdLine= instr(sCmdLine, sScriptName)
if pCmdLine then
select case instr(""":\", _
mid(" " & sCmdLine, pCmdLine, 1))
case 0: if _
(instr(ltrim(replace(sCmdLine, vbTab, " ")), _
sScriptName)=1) then nPID= .processId
case else: nPID= .processId
end select
end if
end if
end with
next

select case nPID
case 0: wscript.echo "The script's PID was not found."
case else: wscript.echo "The script's name is """ & sScriptName _
& """." & vbCr & "The script's PID is " & nPID & "."
end select
---

Regards,
Joe Earnest




---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04