WMI tasks for processes obtain information such as the account under which a process is running. You can perform actions like creating processes. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.
The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.
The following procedure describes how to run a script.
To run a script
- Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
- Open a command prompt window and navigate to the directory where you saved the file.
- Type cscript filename.vbs at the command prompt.
- If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).
Note
By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.
The following table lists script examples that can be used to obtain various types of data from the local computer.
...run an application in a hidden window? |
Call the application from a script that uses the Win32_Process and Win32_ProcessStartup classes.
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)
|
$startup=[wmiclass]"Win32_ProcessStartup"
$startup.Properties['ShowWindow'].value=$False
([wmiclass]"win32_Process").create('notepad.exe','C:\',$Startup)
|
|
...determine which scripts are running on the local computer? |
Use the Win32_Process class and return all processes with the name Cscript.exe or Wscript.exe. To determine the individual scripts running in these processes, check the value of the CommandLine property.
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
|
$strComputer = "."
Get-WmiObject -Class "Win32_Process" -ComputerName "." | `
where {($_.name -eq 'cscript.exe') -or ($_.name -eq 'wscript.exe') } | `
Format-List -Property CommandLine, Name
|
|
...find out the account name under which a process is running? |
Use the Win32_Process class and the GetOwner method.
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
|
Get-WmiObject -class win32_process -ComputerName "." | ForEach-Object { $_.GetOwner() | Select -Property ___domain, user }
|
|
...change the priority of a running process? |
Use the Win32_Process class and the SetPriority method.
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
|
$ABOVE_NORMAL = 32768
$strComputer = "."
$colProcesses = Get-WmiObject -Class Win32_Process -ComputerName $strComputer | Where-Object { $_.name -eq 'Notepad.exe' }
foreach ($objProcess in $colProcesses) { $objProcess.SetPriority($ABOVE_NORMAL) }
|
|
...terminate a process using a script? |
Use the Win32_Process class and the Terminate method.
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
|
$strComputer = "."
$colProcesses = Get-WmiObject -Class Win32_Process -ComputerName $strComputer | Where-Object { $_.name -eq 'Notepad.exe' }
foreach ($objProcess in $colProcesses) { $objProcess.Terminate() }
|
|
...determine how much processor time and memory each process is using? |
Use the Win32_Process class and properties such as KernelModeTime, WorkingSetSize, PageFileUsage, and PageFaults.
strComputer = "."
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
|
$strComputer = "."
Get-WmiObject -Class "Win32_Process" -ComputerName $strComputer | `
Format-List -Property Name, KernelModeTime, UserModeTime, ProcessID, WorkingSetSize, PageFileUsage, PageFaults
|
|
...tell what applications are running on a remote computer? |
Use the Win32_Process class.
strComputer = "atl-dc-01"
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
|
strComputer = "atl-dc-01"
get-wmiObject -class Win32_Process -Namespace "root\cimv2" -ComputerName $strComputer | `
Format-list Name, ProcessID, ThreadCount, PageFileUsage, PageFaults, WorkingSetSize
|
|
-
WMI Tasks for Scripts and Applications
-
WMI C++ Application Examples
-
TechNet ScriptCenter