WMI 工作:計算機硬體
本文內容
計算機硬體的 WMI 工作會取得硬體元件存在、狀態或屬性的相關信息。 例如,您可以判斷計算機是桌面計算機還是膝上型電腦。 如需其他範例,請參閱 TechNet ScriptCenter at https://www.microsoft.com/technet 。
本主題中顯示的腳本範例只會從本機計算機取得數據。 如需如何使用腳本從遠端電腦取得數據的詳細資訊,請參閱 連線 遠端電腦上的 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”} } }
函式 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 - 光學感測器”} }
<# 顯示詳細資料#>
"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
}
範例
「使用 PowerShell 進行多線程系統資產收集」PowerShell 範例會透過 WMI 收集大量有用的系統資訊,並使用 powershell 進行多線程處理。
腳本和應用程式的 WMI 工作
WMI C++ 應用程式範例
TechNet ScriptCenter