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