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
|