(2013-06-05 10:46:46)
原文地址:使用WMI(三)作者:小桃红
八、WMI的应用
这一节将主要分类举出WMI应用的例子,不过暂时不会涉及WMI事件。
No.1.计算机硬件
1.查看可用的物理内存
你可以使用Win32_OperatingSystem类和FreePhysicalMemory属性.
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colSettings = $objWMIService.ExecQuery_
("Select * from Win32_OperatingSystem")
For $objOperatingSystem in$colSettings
ConsoleWrite("可用物理内存: " & _
$objOperatingSystem.FreePhysicalMemory )
Next
2.判断计算机是否有DVD驱动器
使用Win32_CDROMDrive类下的Name或者DeviceID属性。
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colItems = $objWMIService.ExecQuery_
("Select * from Win32_CDROMDrive")
For $objItemin $colItems
ConsoleWrite("Device ID: " &$objItem.DeviceID&@CRLF _
& "Description: "& $objItem.Description &@CRLF _
& "Name: "& $objItem.Name &@CRLF&@CRLF)
Next
3.检查系统信息处理器数目
使用Win32_ComputerSystem类下的NumberOfProcessors属性.
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colSettings = $objWMIService.ExecQuery_
("Select * from Win32_ComputerSystem")
For $objComputerin $colSettings
ConsoleWrite("System Name: " &$objComputer.Name& @CRLF )
ConsoleWrite("Number of Processors: " & _
$objComputer.NumberOfProcessors )
Next
4.检查PCMCIA接口数量
使用Win32_PCMCIAController类下的Count 属性
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\" & $strComputer & "rootcimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_PCMCIAController")
ConsoleWrite("Number of PCMCIA slots: " _
& $colItems.Count)
5.列出没有工作的硬件
你可以使用 Win32_PnPEntity类, 然后通过使用WQL 查询WHERE ConfigManagerErrorCode <> 0 来判断
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\" & $strComputer & "rootcimv2")
$colItems = $objWMIService.ExecQuery_
("Select * from Win32_PnPEntity " _
& "WHERE ConfigManagerErrorCode <> 0")
For $objItemin $colItems
ConsoleWrite("Class GUID: " &$objItem.ClassGuid & @CRLF )
ConsoleWrite("Description: " &$objItem.Description & @CRLF )
ConsoleWrite("Device ID: " &$objItem.DeviceID& @CRLF )
ConsoleWrite("Manufacturer: " &$objItem.Manufacturer & @CRLF )
ConsoleWrite("Name: " & $objItem.Name& @CRLF )
ConsoleWrite("PNP Device ID: " &$objItem.PNPDeviceID & @CRLF )
ConsoleWrite("Service: " & $objItem.Service& @CRLF &@CRLF )
Next
6.列出所有鼠标的信息
使用 Win32_PointingDevice类,这样会列出所有指针设备,而不仅仅是鼠标
.$strComputer = "."
$objWMIService = ObjGet( _
"winmgmts:\" &$strComputer &"rootcimv2")
$colItems = $objWMIService.ExecQuery( _
"Select * from Win32_PointingDevice")
For $objItemin $colItems
ConsoleWrite("Description: " &$objItem.Description & @CRLF )
ConsoleWrite("Device ID: " &$objItem.DeviceID& @CRLF )
ConsoleWrite("Device Interface: " _
& $objItem.DeviceInterface & @CRLF )
ConsoleWrite("Double Speed Threshold: " _
& $objItem.DoubleSpeedThreshold & @CRLF )
ConsoleWrite("Handedness: " &$objItem.Handedness & @CRLF )
ConsoleWrite("Hardware Type: " &$objItem.HardwareType & @CRLF )
ConsoleWrite("INF File Name: " &$objItem.InfFileName & @CRLF )
ConsoleWrite("INF Section: " &$objItem.InfSection & @CRLF )
ConsoleWrite("Manufacturer: " &$objItem.Manufacturer & @CRLF )
ConsoleWrite("Name: " & $objItem.Name& @CRLF )
ConsoleWrite("Number Of Buttons: " _
& $objItem.NumberOfButtons & @CRLF )
ConsoleWrite("PNP Device ID: " &$objItem.PNPDeviceID & @CRLF )
ConsoleWrite("Pointing Type: " &$objItem.PointingType & @CRLF )
ConsoleWrite("Quad Speed Threshold: " _
& $objItem.QuadSpeedThreshold & @CRLF )
ConsoleWrite("Resolution: " &$objItem.Resolution & @CRLF )
ConsoleWrite("Sample Rate: " &$objItem.SampleRate & @CRLF )
ConsoleWrite("Synch: " & $objItem.Synch& @CRLF &@CRLF )
Next
7.判断计算机是台式机还是笔记本
使用 Win32_SystemEnclosure类下的 ChassisType 属性
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colChassis = $objWMIService.ExecQuery_
("Select * from Win32_SystemEnclosure")
For $objChassisin $colChassis
For $objItemin $objChassis.ChassisTypes
ConsoleWrite("Chassis Type: " &$objItem & @CRLF )
Next
Next
8.检查USB接口里插入的是什么设备
使用 Win32_USBHub类并检查 Description 属性
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\" & $strComputer & "rootcimv2")
$colItems = $objWMIService.ExecQuery( _
"Select * from Win32_USBHub")
For $objItemin $colItems
ConsoleWrite("Device ID: " &$objItem.DeviceID& @CRLF )
ConsoleWrite("PNP Device ID: " _
& $objItem.PNPDeviceID & @CRLF )
ConsoleWrite("Description: " _
& $objItem.Description & @CRLF & @CRLF)
Next
9.检查系统有多少Tape设备
使用 Win32_TapeDrive类然后使用 Count途径
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colItems = $objWMIService.ExecQuery( _
"Select * from Win32_TapeDrive")
ConsoleWrite("Number of tape drives: " _
& $colItems.Count & @CRLF)
No.2.计算机软件
1.卸载软件
如果软件是使用Microsoft Windows Installer (MSI) 安装的话,使用Win32_Product类和 Uninstall途径
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colSoftware = $objWMIService.ExecQuery_
("Select * from Win32_Product " _
& "Where Name = 'Personnel database'")
For $objSoftwarein $colSoftware
$objSoftware.Uninstall()
Next
2.列出全部的已安装的软件
如果软件是使用Microsoft Windows Installer (MSI) 安装的话,使用 Win32_Product类
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colSoftware = $objWMIService.ExecQuery_
("Select * from Win32_Product")
For $objSoftwarein $colSoftware
ConsoleWrite("Name: " & $objSoftware.Name& @CRLF )
ConsoleWrite("Version: " & $objSoftware.Version& @CRLF &@CRLF )
Next
3.检查用户安装的Microsoft Office的版本
使用 Win32_Product类下的 Version 属性
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colSoftware = $objWMIService.ExecQuery("Select * from Win32_Product " & _
"Where IdentifyingNumber =" _
& " '{90280409-6000-11D3-8CFE-0050048383C9}'")
For $objItemin $colSoftware
ConsoleWrite("Name: " & $objItem.Name& @CRLF )
ConsoleWrite("Version: " & $objItem.Version& @CRLF &@CRLF )
Next
No.3.桌面管理
1.判断系统是否启动在安全模式的网络状态下下
使用 Win32_ComputerSystem类并检查 PrimaryOwnerName 属性.
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colSettings = $objWMIService.ExecQuery_
("Select * from Win32_ComputerSystem")
For $objComputerin $colSettings
ConsoleWrite("System Name: " _
& $objComputer.Name& @CRLF )
ConsoleWrite("Registered owner: " _
& $objComputer.PrimaryOwnerName& @CRLF )
Next
2.检查屏幕保护是否需要密码
使用 Win32_Desktop类并检查 ScreenSaverSecure 属性.
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\" & $strComputer & "rootcimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_Desktop")
For $objItemin $colItems
ConsoleWrite("Screen Saver Secure: " _
& $objItem.ScreenSaverSecure & @CRLF )
Next
3.获取屏幕分辨率
使用 Win32_DesktopMonitor类并检查 ScreenHeight 和 ScreenWidth属性.
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\" & $strComputer & "rootcimv2")
$colItems = $objWMIService.ExecQuery( "Select * from Win32_DesktopMonitor")
For $objItemin $colItems
ConsoleWrite("Screen Height: " _
& $objItem.ScreenHeight & @CRLF )
ConsoleWrite("Screen Width: " _
& $objItem.ScreenWidth & @CRLF )
Next
4.获取系统开机的时间
使用 Win32_OperatingSystem类的 LastBootUpTime 属性.
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colOperatingSystems = $objWMIService.ExecQuery_
("Select * from Win32_OperatingSystem")
For $objOSin $colOperatingSystems
ConsoleWrite($objOS.LastBootUpTime)
Next
.5.重启或关闭远程计算机
使用 Win32_OperatingSystem类以及 Win32Shutdown途径
$strComputer = "atl-dc-01"
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\"& _
$strComputer &"rootcimv2")
$colOperatingSystems = $objWMIService.ExecQuery_
("Select * from Win32_OperatingSystem")
For $objOperatingSystem in$colOperatingSystems
$ObjOperatingSystem.Shutdown(1)
Next
6.检查开机自启动项
使用 Win32_StartupCommand类
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& $strComputer& "rootcimv2")
$colStartupCommands = $objWMIService.ExecQuery_
("Select * from Win32_StartupCommand")
For $objStartupCommand in$colStartupCommands
ConsoleWrite("Command: " & $objStartupCommand.Command& @CRLF _
& "Description: "& $objStartupCommand.Description & @CRLF _
& "Location: "& $objStartupCommand.Location& @CRLF _
& "Name: "& $objStartupCommand.Name& @CRLF _
& "SettingID: "& $objStartupCommand.SettingID& @CRLF _
& "User: "& $objStartupCommand.User& @CRLF &@CRLF )
Next
联系客服