WMI 任务:进程
本文内容
进程的 WMI 任务会获取信息,例如运行进程的帐户。 可以执行创建进程之类的操作。 有关其他示例,请通过 https://www.microsoft.com/technet 访问 TechNet ScriptCenter。
本主题中所示的脚本示例仅从本地计算机获取数据。 有关如何使用脚本从远程计算机获取数据的详细信息,请参阅连接到远程计算机上的 WMI 。
以下过程介绍了如何运行脚本。
运行脚本
复制代码并将其保存在扩展名为 .vbs 的文件中,例如 filename.vbs。 确保文本编辑器不会向该文件添加 .txt 扩展名。
打开命令提示符窗口并导航到保存该文件的目录。
在命令提示符下键入 cscript filename.vbs。
如果无法访问事件日志,请进行检查以查看是否正从提升的命令提示符运行。 某些事件日志(例如安全事件日志)可能受用户访问控制 (UAC) 的保护。
注意
默认情况下,cscript 会在命令提示符窗口中显示脚本的输出。 由于 WMI 脚本可以生成大量输出,因此可能需要将输出重定向到文件。 在命令提示符下键入 cscript filename.vbs > outfile.txt 以将 filename.vbs 脚本的输出重定向到 outfile.txt。
下表列出了可用于从本地计算机获取各种类型的数据的脚本示例。
...在隐藏的窗口中运行应用程序?
从使用 Win32_Process 和 Win32_ProcessStartup 类的脚本调用应用程序。
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)
...确定在本地计算机上运行的脚本?
使用 Win32_Process 类,返回名为 Cscript.exe 或 Wscript.exe 的所有进程。 若要确定在这些进程中运行的各个脚本,请检查 CommandLine 属性的值。
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
...找出运行进程的帐户名称?
使用 Win32_Process 类和 GetOwner 方法。
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 }
...更改正在运行的进程的优先级?
使用 Win32_Process 类和 SetPriority 方法。
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) }
...使用脚本终止进程?
使用 Win32_Process 类和 Terminate 方法。
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() }
...确定每个进程使用的处理器时间和内存量?
使用 Win32_Process 类和属性,例如 KernelModeTime、WorkingSetSize、PageFileUsage 和 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 "Win32s_Process" -ComputerName $strComputer | `
Format-List -Property Name, KernelModeTime, UserModeTime, ProcessID, WorkingSetSize, PageFileUsage, PageFaults
...告知远程计算机上正在运行哪些应用程序?
使用 Win32_Process 类。
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 任务
WMI C++ 应用程序示例
TechNet ScriptCenter