Group:  English: Windows Server ยป microsoft.public.windows.server.scripting
Thread: How to get the pid of the current running script and parent proces

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

How to get the pid of the current running script and parent proces
Wizard 11.07.2007 23:56:00
Sorry, I am not a power shell user (YET). I am using cscript

I have tried the following and it did not work for the oShell.ProcessID.

When I exec the process, it returns the PID.... Why?


How do I get the current process ID, so I can perform actions on all of the
processes in the system but the current process AND some of the other
selected processes like windows itself etc.

var oShell = WScript.CreateObject( "WScript.shell" );
var oNet = WScript.CreateObject( "WScript.Network" );
var strComputer = oNet.ComputerName;
WScript.Echo( "The computer name is ",strComputer);
WScript.Echo( "The PID is ",oShell.ProcessID);
var oCalc = oShell.Exec("calc");
WScript.Echo( "The PID is ",oCalc.ProcessID);


--
John
Re: How to get the pid of the current running script and parent proces
"Michael Harris \(MVP\)" <mikhar.at.mvps.dot.org> 12.07.2007 01:04:34
Wizard wrote:
[Quoted Text]
> Sorry, I am not a power shell user (YET). I am using cscript
>
> I have tried the following and it did not work for the
> oShell.ProcessID.
>
> When I exec the process, it returns the PID.... Why?

Because that is simply how the object returned by the WshShell.Exec method
is designed...

>
>
> How do I get the current process ID, so I can perform actions on all
> of the processes in the system but the current process AND some of
> the other selected processes like windows itself etc.
>
> var oShell = WScript.CreateObject( "WScript.shell" );
> var oNet = WScript.CreateObject( "WScript.Network" );
> var strComputer = oNet.ComputerName;
> WScript.Echo( "The computer name is ",strComputer);
> WScript.Echo( "The PID is ",oShell.ProcessID);
> var oCalc = oShell.Exec("calc");
> WScript.Echo( "The PID is ",oCalc.ProcessID);

On XP and later, the Win32_Process objects that you can get with an
ExecQuery call have a CommandLine property along with the ProcessId (same as
Handle) and ExecutablePath from pre-XP WMI versions. Note that CommandLine
and Executable path may be Null on some Win32_Process instances.

This is a vbscript example but easy enough to port to jscript...

Set wmi = GetObject("winmgmts://./root/cimv2")
Set processes = wmi.execQuery(_
"select Handle, Caption, CommandLine, ExecutablePath " _
& "from win32_process")
For Each process In processes
with process
wscript.echo .Handle, .Caption, .CommandLine, .ExecutablePath
End with
Next

--
Michael Harris
MVP - Admin Frameworks


Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net