Bewerken

Delen via


WMI Tasks: Computer Hardware

WMI tasks for computer hardware obtain information about the presence, state, or properties of hardware components. For example, you can determine whether a computer is a desktop or laptop. 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.

To run a script

The following procedure describes how to run a script.

  1. 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.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. 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.

How do I... WMI classes or methods
...determine how much free memory a computer has? Use the class Win32_OperatingSystem and the FreePhysicalMemory property.
VB
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
PowerShell
$mem = Get-WmiObject -Class Win32_OperatingSystem
"System : {0}" -f $mem.csname
"Free Memory: {0}" -f $mem.FreePhysicalMemory
...determine whether a computer has a DVD drive?

Use the Win32_CDROMDrive class and check for the acronym DVD in the Name or DeviceID property.

VB
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
PowerShell
$drives = Get-WmiObject -Class Win32_CDROMDrives
$drives | Format-Table DeviceID, Description, Name -autosize
...determine how much RAM is installed in a computer?

Use the Win32_ComputerSystem class and check the value of the TotalPhysicalMemory property.

VB
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
PowerShell
$mem = Get-WmiObject -Class Win32_ComputerSystem
...determine if a computer has more than one processor?

Use the Win32_ComputerSystem class and the property NumberOfProcessors.

VB
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
PowerShell
"System Name : {0}" -f $system.Name
"Number of Processors: {0}" -f $system.NumberOfProcessors
...determine whether a computer has a PCMCIA slot?

Use the Win32_PCMCIAController class and check the value of the Count property. If Count is 0, then the computer has no PCMCIA slots.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PCMCIAController")
Wscript.Echo "Number of PCMCIA slots: " & colItems.Count

PowerShell
$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 }

...identify devices that are not working (those marked with an exclamation point icon in Device Manager)?

Use the Win32_PnPEntity class and use the following clause in your WQL query. WHERE ConfigManagerErrorCode <> 0 Note that this code may not detect USB devices that are missing drivers.

VB
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
PowerShell
$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
    ""
}
...determine the properties of the mouse used on computer?

Use the Win32_PointingDevice class. This returns the properties of all pointing devices, not just mouse devices.

VB
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

PowerShell
# Get Mouse information

$mouse = Get-WmiObject -Class Win32_PointingDevice

<# Decode detalis #>

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"} } }

<# Display details #>

"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

...determine the speed of a processor installed in a computer?

Use the Win32_Processor class and check the value of the MaxClockSpeed property.

VB
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
...determine whether a computer is a tower, a mini-tower, a laptop, and so on?

Use the Win32_SystemEnclosure class and check the value of the ChassisType property.

VB
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
PowerShell
$processors = Get-WmiObject -Class Win32_Processor
foreach ($proc in $processors)
{
    "Processor ID: " + $proc.ProcessorID
    "Maximum Clock Speed: " + $proc.MaxClockSpeed
}
...get the serial number and asset tag of a computer?

Use the Win32_SystemEnclosure class, and the properties SerialNumber and SMBIOSAssetTag.

VB
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

PowerShell
$colSMBIOS = Get-WmiObject -Class Win32_SystemEnclosure

foreach ($objSMBIOS in $colSMBIOS) { "Part Number: " + $objSMBIOS.PartNumber "Serial Number: " + $objSMBIOS.SerialNumber "Asset Tag: " + $objSMBIOS.SMBIOSAssetTag }

...determine what kind of device is plugged into a USB port?

Use the Win32_USBHub class and check the Description property. This property may have a value such as "Mass Storage Device" or "Printing Support".

VB
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

PowerShell
$colItems = Get-WmiObject -Class Win32_USBHub

foreach ($objItem in $colItems) { "Device ID: " + $objItem.DeviceID "PNP Device ID: " + $objItem.PNPDeviceID "Description: " + $objItem.Description }

...determine how many tape drives are installed on a computer?

Use the class Win32_TapeDrive class and then use the SWbemObjectSet.Count method. If Count = 0, then no tape drives are installed on the computer.

VB
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

PowerShell
$colItems = Get-WmiObject -Class Win32_TapeDrive

foreach ($objItem in $colItems) { "Number of Drives: " + $colItems.Count }

Examples

The "Multithreaded System Asset Gathering with PowerShell" PowerShell sample gathers a plethora of useful system information via WMI and multithreading with powershell.

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter