0% found this document useful (0 votes)
98 views

WMI Tasks

WMI tasks for processes obtain information about running processes like the account under which they run. Scripts using WMI classes like Win32_Process can get process details, create and terminate processes, change process priorities, and get resource usage of processes on the local or remote computers. The document provides examples of using various WMI methods and classes to interact with and monitor processes.

Uploaded by

Daniel Esposto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

WMI Tasks

WMI tasks for processes obtain information about running processes like the account under which they run. Scripts using WMI classes like Win32_Process can get process details, create and terminate processes, change process priorities, and get resource usage of processes on the local or remote computers. The document provides examples of using various WMI methods and classes to interact with and monitor processes.

Uploaded by

Daniel Esposto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

WMI Tasks: Processes

WMI tasks for processes obtain information such as the account under which a process is running. You

can carry out actions like creating processes. For other examples, see the TechNet ScriptCenter at

http://www.microsoft.com/technet.

The script examples shown in this topic obtain data only from the local computer. For information on

how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote

Computer.

You can run the scripts shown below by copying the code to a file with a .vbs extension then executing

the command line cscript file.vbs. WMI scripts often produce a large amount of output which can be

redirected to a text file: cscript scriptfile.vbs > textfile.txt.

How do I... WMI classes or methods

...run an Call the application from a script that uses the Win32_Process and
application in a Win32_ProcessStartup classes.
hidden window?
Const HIDDEN_WINDOW = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject( _
"winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create( _
"Notepad.exe", null, objConfig, intProcessID)

...determine which Use the Win32_Process class and return all processes with the
scripts are running name Cscript.exe or Wscript.exe. To determine the individual scripts
on the local running in these processes, check the value of the CommandLine
computer? property.
Windows 2000/NT and Windows 98/95: The CommandLine
property is not available.
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Process" & _
" WHERE Name = 'cscript.exe'" & _
" OR Name = 'wscript.exe'",,48)
For Each objItem in colItems
Wscript.Echo
"-------------------------------------------"
Wscript.Echo "CommandLine: " & objItem.CommandLine
Wscript.Echo "Name: " & objItem.Name
Next

...find out the Use the Win32_Process class and the GetOwner method.
account name Windows 2000/NT and Windows 98/95: The GetOwner
under which a method is not available.
process is running?
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process")
For Each objProcess in colProcessList
colProperties = objProcess.GetOwner( _
strNameOfUser,strUserDomain)
Wscript.Echo "Process " & objProcess.Name _
& " is owned by " _
& strUserDomain & "\" & strNameOfUser & "."
Next

...change the Use the Win32_Process class and the SetPriority method.
priority of a Windows 2000/NT and Windows 98/95: The SetPriority
running process? method is not available.
Const ABOVE_NORMAL = 32768
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name =
'Notepad.exe'")
For Each objProcess in colProcesses
objProcess.SetPriority(ABOVE_NORMAL)
Next

...terminate a Use the Win32_Process class and the Terminate method.


process using a Windows NT and Windows 98/95: The Terminate method is not
script? available.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name =
'Notepad.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

...determine how Use the Win32_Process class and properties such as


much processor KernelModeTime, WorkingSetSize, PageFileUsage, and
time and memory PageFaults.
each process is
strComputer = "."
using?
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process")
For Each objProcess in colProcesses

Wscript.Echo "Process: " & objProcess.Name


sngProcessTime = (CSng(objProcess.KernelModeTime) + _
CSng(objProcess.UserModeTime)) / 10000000
Wscript.Echo "Processor Time: " & sngProcessTime
Wscript.Echo "Process ID: " & objProcess.ProcessID
Wscript.Echo "Working Set Size: " _
& objProcess.WorkingSetSize
Wscript.Echo "Page File Size: " _
& objProcess.PageFileUsage
Wscript.Echo "Page Faults: " & objProcess.PageFaults
Next

...tell what Use the Win32_Process class.


applications are
strComputer = "atl-dc-01"
running on a
remote computer? Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process")
For Each objProcess in colProcessList
Wscript.Echo "Process: " & objProcess.Name
Wscript.Echo "Process ID: " & objProcess.ProcessID
Wscript.Echo "Thread Count: " & objProcess.ThreadCount
Wscript.Echo "Page File Size: " _
& objProcess.PageFileUsage
Wscript.Echo "Page Faults: " _
& objProcess.PageFaults
Wscript.Echo "Working Set Size: " _
& objProcess.WorkingSetSize
Next

You might also like