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_OperatingSystem 和 FreePhysicalMemory 属性。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
Wscript.Echo "Available Physical Memory: " & objOperatingSystem.FreePhysicalMemory
Next
$mem = Get-WmiObject -Class Win32_OperatingSystem
"System : {0}" -f $mem.csname
"Free Memory: {0}" -f $mem.FreePhysicalMemory
...确定计算机是否有 DVD 驱动器?
使用 Win32_CDROMDrive 类,并在 Name 或 DeviceID 属性中检查首字母缩略词 DVD。
strComputer = "."
Set objWMIService = GetObject( "winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Name: " & objItem.Name
Next
$drives = Get-WmiObject -Class Win32_CDROMDrives
$drives | Format-Table DeviceID, Description, Name -autosize
...确定计算机中安装了多少 RAM?
使用 Win32_ComputerSystem 类并检查 TotalPhysicalMemory 属性的值。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
Wscript.Echo "System Name: " & objComputer.Name
Wscript.Echo "Total Physical Memory: " & objComputer.TotalPhysicalMemory
Next
$mem = Get-WmiObject -Class Win32_ComputerSystem
...确定计算机是否有多个处理器?
使用 Win32_ComputerSystem 类和属性 NumberOfProcessors。
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
Wscript.Echo "System Name: " & objComputer.Name
Wscript.Echo "Number of Processors: " & objComputer.NumberOfProcessors
Next
"System Name : {0}" -f $system.Name
"Number of Processors: {0}" -f $system.NumberOfProcessors
...确定计算机是否有 PCMCIA 插槽?
使用 Win32_PCMCIAController 类并检查 Count 属性的值。 如果 Count 为 0,则计算机没有 PCMCIA 槽。
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PCMCIAController")
Wscript.Echo "Number of PCMCIA slots: " & colItems.Count
$Pcmcia = Get-WmiObject -Class Win32_PCMCIAController
if (!$pcmcia.count) {
"Number of PCMCIA Slots: {0}" -f 1
}else {
"Number of PCMCIA Slots: {0}" -f $pcmcia.count
}
...识别不正常工作的设备(在“设备管理器”中标有感叹号图标)?
使用 Win32_PnPEntity 类,并在 WQL 查询中使用以下子句 。 WHERE ConfigManagerErrorCode <> 0 请注意,此代码可能无法检测缺少驱动程序的 USB 设备。
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PnPEntity WHERE ConfigManagerErrorCode <> 0")
For Each objItem in colItems
Wscript.Echo "Class GUID: " & objItem.ClassGuid
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID
Wscript.Echo "Service: " & objItem.Service
Next
$baddevices = Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}
" Total Bad devices: {0}" -f $baddevices.count
foreach ($device in $baddevices) {
"Name : {0}" -f $device.name
"Class Guid : {0}" -f $device.Classguid
"Description : {0}" -f $device.Description
"Device ID : {0}" -f $device.deviceid
"Manufacturer : {0}" -f $device.manufactuer
"PNP Devcice Id : {0}" -f $device.PNPDeviceID
"Service Name : {0}" -f $device.service
""
}
...确定计算机上使用的鼠标的属性?
使用 Win32_PointingDevice 类。 这将返回所有指针设备(而不仅仅是鼠标设备)的属性。
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PointingDevice")
For Each objItem in colItems
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "Device Interface: " & objItem.DeviceInterface
Wscript.Echo "Double Speed Threshold: " & objItem.DoubleSpeedThreshold
Wscript.Echo "Handedness: " & objItem.Handedness
Wscript.Echo "Hardware Type: " & objItem.HardwareType
Wscript.Echo "INF File Name: " & objItem.InfFileName
Wscript.Echo "INF Section: " & objItem.InfSection
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Number Of Buttons: " & objItem.NumberOfButtons
Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID
Wscript.Echo "Pointing Type: " & objItem.PointingType
Wscript.Echo "Quad Speed Threshold: " & objItem.QuadSpeedThreshold
Wscript.Echo "Resolution: " & objItem.Resolution
Wscript.Echo "Sample Rate: " & objItem.SampleRate
Wscript.Echo "Synch: " & objItem.Synch
Next
# 获取鼠标信息$mouse = Get-WmiObject -Class Win32_PointingDevice
<# 解码详细信息 #>
function Deviceinterface { param ($value) switch ($value) { 0 {"Other"} 1 {"Unknown"} 3 {"Serial"} 4 {"PS/2"} 5 {"Infrared"} 6 {"HP-HIL"} 7 {"Bus Mouse"} 8 {"ADP (Apple Desktop Bus)"} 160 {"Bus Mouse DB-9"} 161 {"Bus Mouse Micro-DIN"} 162 {"USB"} } }
function Handedness { param ($value) switch ($value) { 0 {"Unknown"} 1 {"Not Applicable"} 2 {"Right-Handed Operation"} 3 {"Left-Handed Operation"} } }
function Pointingtype {
param ($value) switch ($value) { 1 {"Other"} 2 {"Unknown"} 3 {"Mouse"} 4 {"Track Ball"} 5 {"Track Point"} 6 {"Glide Point"} 7 {"Touch Pad"} 8 {"Touch Screen"} 9 {"Mouse - Optical Sensor"} } }
<# 显示详细信息 #>
"Mouse Information on System: {0}" -f $mouse.systemname
"Description : {0}" -f $mouse.Description
"Device ID : {0}" -f $mouse.DeviceID
"Device Interface : {0}" -f (Deviceinterface($mouse.DeviceInterface))
"Double Speed Threshold : {0}" -f $mouse.DoubleSpeedThreshold
"Handedness : {0}" -f (Handedness($mouse.handedness))
"Hardware Type : {0}" -f $mouse.Hardwaretype
"INF FIle Name : {0}" -f $mouse.InfFileName
"Inf Section : {0}" -f $mouse.InfSection
"Manufacturer : {0}" -f $mouse.Manufacturer
"Name : {0}" -f $mouse.Name
"Number of buttons : {0}" -f $mouse.NumberOfButtons
"PNP Device ID : {0}" -f $mouse.PNPDeviceID
"Pointing Type : {0}" -f (Pointingtype ($mouse.PointingType))
"Quad Speed Threshold : {0}" -f $mouse.QuadSpeedThreshold
"Resolution : {0}" -f $mouse.Resolution
"Sample Rate : {0}" -f $mouse.SampleRate
"Synch : {0}" -f $mouse.Synch
...确定计算机上安装的处理器的速度?
使用 Win32_Processor 类并检查 MaxClockSpeed 属性的值。
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem in colItems
Wscript.Echo "Processor Id: " & objItem.ProcessorId
Wscript.Echo "Maximum Clock Speed: " & objItem.MaxClockSpeed
Next
...确定计算机是否为塔式服务器、微型塔式服务器、便携式计算机等?
使用 Win32_SystemEnclosure 类并检查 ChassisType 属性的值。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
For Each objChassis in colChassis
For Each objItem in objChassis.ChassisTypes
Wscript.Echo "Chassis Type: " & objItem
Next
Next
$processors = Get-WmiObject -Class Win32_Processor
foreach ($proc in $processors)
{
"Processor ID: " + $proc.ProcessorID
"Maximum Clock Speed: " + $proc.MaxClockSpeed
}
...获取计算机的序列号和资产标记?
使用 Win32_SystemEnclosure 类以及属性 SerialNumber 和 SMBIOSAssetTag。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
Wscript.Echo "Part Number: " & objSMBIOS.PartNumber
Wscript.Echo "Serial Number: " & objSMBIOS.SerialNumber
Wscript.Echo "Asset Tag: " & objSMBIOS.SMBIOSAssetTag
Next
$colSMBIOS = Get-WmiObject -Class Win32_SystemEnclosure
foreach ($objSMBIOS in $colSMBIOS)
{
"Part Number: " + $objSMBIOS.PartNumber
"Serial Number: " + $objSMBIOS.SerialNumber
"Asset Tag: " + $objSMBIOS.SMBIOSAssetTag
}
...确定已将哪种设备插入 USB 端口?
使用 Win32_USBHub 类并检查 Description 属性。 此属性可能具有“大容量存储设备”或“打印支持”等值。
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_USBHub")
For Each objItem in colItems
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo
Next
$colItems = Get-WmiObject -Class Win32_USBHub
foreach ($objItem in $colItems)
{
"Device ID: " + $objItem.DeviceID
"PNP Device ID: " + $objItem.PNPDeviceID
"Description: " + $objItem.Description
}
...确定计算机上安装了多少个磁带驱动器?
使用 Win32_TapeDrive 类,然后使用 SWbemObjectSet.Count 方法。 如果 Count = 0,则计算机上未安装磁带驱动器。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_TapeDrive")
Wscript.Echo "Number of tape drives: " & colItems.Count
$colItems = Get-WmiObject -Class Win32_TapeDrive
foreach ($objItem in $colItems)
{
"Number of Drives: " + $colItems.Count
}
示例
以下 TechNet 库示例代码 介绍了如何列出多台计算机的所有驱动器的可用空间。
TechNet 库中的 Get-ComputerInfo - 从本地/远程计算机查询计算机信息 - (WMI) PowerShell 示例使用了大量的硬件和软件调用来显示有关本地或远程系统的信息。
TechNet 库中的使用 PowerShell 收集多线程系统资产 PowerShell 示例使用 PowerShell 通过 WMI 和多线程收集大量有用的系统信息。
脚本和应用程序的 WMI 任务
WMI C++ 应用程序示例
TechNet ScriptCenter