八、WMI 的应用
这一节将主要分类举出WMI 应用的例子,不过暂时不会涉及WMI 事件。
No.1.计算机硬件
1.查看可用的物理内存
你可以使用Win32_OperatingSystem 类和FreePhysicalMemory 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colSettings = $objWMI Service .ExecQuery _ ( "Select * from Win32_OperatingSystem" ) For $objOperatingSystem in $colSettings ConsoleWrite ( "可用物理内存: " & _ $objOperatingSystem .FreePhysicalMemory ) Next
2.判断计算机是否有DVD驱动器
使用Win32_CDROMDrive 类下的Name 或者DeviceID 属性。
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery _ ( "Select * from Win32_CDROMDrive" ) For $objItem in $colItems ConsoleWrite ( "Device ID: " & $objItem .DeviceID &@CRLF _ & "Description: " & $objItem .Description &@CRLF _ & "Name: " & $objItem .Name &@CRLF &@CRLF ) Next
3.检查系统信息处理器数目
使用Win32_ComputerSystem 类下的NumberOfProcessors 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colSettings = $objWMI Service .ExecQuery _ ( "Select * from Win32_ComputerSystem" ) For $objComputer in $colSettings ConsoleWrite ( "System Name: " & $objComputer .Name & @CRLF ) ConsoleWrite ( "Number of Processors: " & _ $objComputer .NumberOfProcessors ) Next
4.检查PCMCIA接口数量
使用Win32_PCMCIAController 类下的 Count 属性
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery ( "Select * from Win32_PCMCIAController" ) ConsoleWrite ( "Number of PCMCIA slots: " _ & $colItems .Count )
5.列出没有工作的硬件
你可以使用 Win32_PnPEntity 类, 然后通过使用WQL 查询 WHERE ConfigManagerErrorCode <> 0 来判断
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery _ ( "Select * from Win32_PnPEntity " _ & "WHERE ConfigManagerErrorCode <> 0" ) For $objItem in $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 = "." $objWMI Service = ObjGet ( _ "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery ( _ "Select * from Win32_PointingDevice" ) For $objItem in $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 = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colChassis = $objWMI Service .ExecQuery _ ( "Select * from Win32_SystemEnclosure" ) For $objChassis in $colChassis For $objItem in $objChassis .ChassisTypes ConsoleWrite ( "Chassis Type: " & $objItem & @CRLF ) Next Next
8.检查USB接口里插入的是什么设备
使用 Win32_USBHub 类并检查 Description 属性
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery ( _ "Select * from Win32_USBHub" ) For $objItem in $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 = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery ( _ "Select * from Win32_TapeDrive" ) ConsoleWrite ( "Number of tape drives: " _ & $colItems .Count & @CRLF )
No.2.计算机软件
1.卸载软件
如果软件是使用Microsoft Windows Installer (MSI) 安装的话,使用 Win32_Product 类和 Uninstall 途径
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colSoftware = $objWMI Service .ExecQuery _ ( "Select * from Win32_Product " _ & "Where Name = 'Personnel database'" ) For $objSoftware in $colSoftware $objSoftware .Uninstall ( ) Next
2.列出全部的已安装的软件
如果软件是使用Microsoft Windows Installer (MSI) 安装的话,使用 Win32_Product 类
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colSoftware = $objWMI Service .ExecQuery _ ( "Select * from Win32_Product" ) For $objSoftware in $colSoftware ConsoleWrite ( "Name: " & $objSoftware .Name & @CRLF ) ConsoleWrite ( "Version: " & $objSoftware .Version & @CRLF & @CRLF ) Next
3.检查用户安装的Microsoft Office的版本
使用 Win32_Product 类下的 Version 属性
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colSoftware = $objWMI Service .ExecQuery ( "Select * from Win32_Product " & _ "Where IdentifyingNumber =" _ & " '{90280409-6000-11D3-8CFE-0050048383C9}'" ) For $objItem in $colSoftware ConsoleWrite ( "Name: " & $objItem .Name & @CRLF ) ConsoleWrite ( "Version: " & $objItem .Version & @CRLF & @CRLF ) Next
No.3.桌面管理
1.判断系统是否启动在安全模式的网络状态下下
使用 Win32_ComputerSystem 类并检查 PrimaryOwnerName 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colSettings = $objWMI Service .ExecQuery _ ( "Select * from Win32_ComputerSystem" ) For $objComputer in $colSettings ConsoleWrite ( "System Name: " _ & $objComputer .Name & @CRLF ) ConsoleWrite ( "Registered owner: " _ & $objComputer .PrimaryOwnerName & @CRLF ) Next
2.检查屏幕保护是否需要密码
使用 Win32_Desktop 类并检查 ScreenSaverSecure 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery ( "Select * from Win32_Desktop" ) For $objItem in $colItems ConsoleWrite ( "Screen Saver Secure: " _ & $objItem .ScreenSaverSecure & @CRLF ) Next
3.获取屏幕分辨率
使用 Win32_DesktopMonitor 类并检查 ScreenHeight 和 ScreenWidth 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery ( "Select * from Win32_DesktopMonitor" ) For $objItem in $colItems ConsoleWrite ( "Screen Height: " _ & $objItem .ScreenHeight & @CRLF ) ConsoleWrite ( "Screen Width: " _ & $objItem .ScreenWidth & @CRLF ) Next
4.获取系统开机的时间
使用 Win32_OperatingSystem 类的 LastBootUpTime 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colOperatingSystems = $objWMI Service .ExecQuery _ ( "Select * from Win32_OperatingSystem" ) For $objOS in $colOperatingSystems ConsoleWrite ( $objOS .LastBootUpTime ) Next
.5.重启或关闭远程计算机
使用 Win32_OperatingSystem 类以及 Win32Shutdown 途径
$strComputer = "atl-dc-01" $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _ $strComputer & "\root\cimv2" ) $colOperatingSystems = $objWMI Service .ExecQuery _ ( "Select * from Win32_OperatingSystem" ) For $objOperatingSystem in $colOperatingSystems $ObjOperatingSystem .Shutdown ( 1 ) Next
6.检查开机自启动项
使用 Win32_StartupCommand 类
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colStartupCommands = $objWMI Service .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
.进程
1.检查系统同时运行了多少个au3脚本
使用 Win32_Process 类并找出名字为AutoIt3.exe的进程.
$strComputer = "." $objWMI Service = ObjGet ( _ "winmgmts:\\" & $strComputer & "\root\CIMV2" ) $colItems = $objWMI Service .ExecQuery ( _ "SELECT * FROM Win32_Process" & _ " WHERE Name = 'AutoIt3.exe'" ) For $objItem in $colItems ConsoleWrite ( "-------------------------------------------" & @CRLF ) ConsoleWrite ( "CommandLine: " & $objItem .CommandLine & @CRLF ) ConsoleWrite ( "Name: " & $objItem .Name & @CRLF & @CRLF ) Next
2.修改进程的优先权
使用 Win32_Process 类和 SetPriority 途径
Const $ABOVE_NORMAL = 32768 $strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colProcesses = $objWMI Service .ExecQuery _ ( "Select * from Win32_Process Where Name = 'Notepad.exe'" ) For $objProcess in $colProcesses $objProcess .SetPriority ( $ABOVE_NORMAL ) Next
3.列出每个进程所占用的内存
使用 Win32_Process 类和诸如 KernelModeTime , WorkingSetSize , PageFileUsage , 与 PageFaults 的属性
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colProcesses = $objWMI Service .ExecQuery _ ( "Select * from Win32_Process" ) For $objProcess in $colProcesses ConsoleWrite ( "Process: " & $objProcess .Name & @CRLF ) $sngProcessTime = ( String ( $objProcess .KernelModeTime ) + _ String ( $objProcess .UserModeTime ) ) / 10000000 ConsoleWrite ( "Processor Time: " & $sngProcessTime & @CRLF ) ConsoleWrite ( "Process ID: " & $objProcess .ProcessID & @CRLF ) ConsoleWrite ( "Working $Size: " _ & $objProcess .WorkingSetSize & @CRLF ) ConsoleWrite ( "Page File Size: " _ & $objProcess .PageFileUsage & @CRLF ) ConsoleWrite ( "Page Faults: " & $objProcess .PageFaults & @CRLF & @CRLF ) Next
No.5.磁盘和文件系统
1.列出每个用户所占用的磁盘空间
使用 Win32_DiskQuota 类和 User 以及 DiskSpaceUsed 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colQuotas = $objWMI Service .ExecQuery _ ( "Select * from Win32_DiskQuota" ) For $objQuota in $colQuotas ConsoleWrite ( "Volume: " & @Tab _ & $objQuota .QuotaVolume & @CRLF ) ConsoleWrite ( "User: " & @Tab & $objQuota .User & @CRLF ) ConsoleWrite ( "Disk Space Used: " _ & @Tab & $objQuota .DiskSpaceUsed & @CRLF ) Next
2.检查软驱里是否有软盘
使用 Win32_LogicalDisk 类并检查 FreeSpace 属性
$strComputer = "." $objWMI Service = ObjGet ( _ "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colItems = $objWMI Service .ExecQuery _ ( "Select * From Win32_LogicalDisk Where DeviceID = 'A:'" ) For $objItem in $colItems $intFreeSpace = $objItem .FreeSpace If $intFreeSpace = "" Then ConsoleWrite ( "There is no disk in the floppy drive." & @CRLF ) Else ConsoleWrite ( "There is a disk in the floppy drive." & @CRLF ) EndIf Next
3.判断磁盘是否为可移动驱动器
使用 Win32_LogicalDisk 类并检查 DriveType 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colDisks = $objWMI Service .ExecQuery _ ( "Select * from Win32_LogicalDisk" ) For $objDisk in $colDisks ConsoleWrite ( "DeviceID: " & @Tab _ & $objDisk .DeviceID & @CRLF ) Switch $objDisk .DriveType Case 1 ConsoleWrite ( "No root directory. " _ & "Drive type could not be " _ & "determined." & @CRLF ) Case 2 ConsoleWrite ( "DriveType: " & @Tab _ & "Removable drive." & @CRLF ) Case 3 ConsoleWrite ( "DriveType: " & @Tab _ & "Local hard disk." & @CRLF ) Case 4 ConsoleWrite ( "DriveType: " & @Tab _ & "Network disk." & @CRLF ) Case 5 ConsoleWrite ( "DriveType: " & @Tab _ & "Compact disk." & @CRLF ) Case 6 ConsoleWrite ( "DriveType: " & @Tab _ & "RAM disk." & @CRLF ) Case Else ConsoleWrite ( "Drive type could not be" _ & " determined." & @CRLF ) EndSwitch Next
4.检查驱动器的文件系统类型
使用 Win32_LogicalDisk 类和 FileSystem 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colDisks = $objWMI Service .ExecQuery _ ( "Select * from Win32_LogicalDisk" ) For $objDisk in $colDisks ConsoleWrite ( "DeviceID: " & $objDisk .DeviceID & @CRLF ) ConsoleWrite ( "File System: " _ & $objDisk .FileSystem & @CRLF ) Next
5.检查磁盘的可用空间
使用 Win32_LogicalDisk 类和 FreeSpace 属性.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\cimv2" ) $colDisks = $objWMI Service .ExecQuery _ ( "Select * from Win32_LogicalDisk" ) For $objDisk in $colDisks ConsoleWrite ( "DeviceID: " & $objDisk .DeviceID & @CRLF ) ConsoleWrite ( "Free Disk Space: " _ & $objDisk .FreeSpace & @CRLF ) Next
6.进行磁盘整理
使用 Win32_Volume 类和 Defrag 途径.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:\\" _ & $strComputer & "\root\cimv2" ) $colVolumes = $objWMI Service .ExecQuery ( "Select * from Win32_Volume Where Name = 'K:\\'" ) For $objVolume in $colVolumes $errResult = $objVolume .Defrag ( ) Next
No.6.网络
1.禁用网络连接
使用 Win32_NetworkAdapterConfiguration 和 ReleaseDHCPLease 途径.
$strComputer = "." $objWMI Service = ObjGet ( "winmgmts:\\" & $strComputer & "\root\cimv2" ) $colNetCards = $objWMI Service .ExecQuery _ ( "Select * From Win32_NetworkAdapterConfiguration " _ & "Where IPEnabled = True"; ) For $objNetCard in $colNetCards $objNetCard .ReleaseDHCPLease ( ) Next
2.得到适配器信息
使用 Win32_NetworkAdapterConfiguration 类.
$strComputer = "." $objWMI Service = ObjGet ( _ "winmgmts:\\" & $strComputer & "\root\cimv2" ) $IPConfigSet = $objWMI Service .ExecQuery ( "Select IPAddress from Win32_NetworkAdapterConfiguration" _ & " where IPEnabled=TRUE" ) For $IPConfig in $IPConfigSet If Not $IPConfig .IPAddress Then For $i =0 To UBound ( $IPConfig .IPAddress ) ConsoleWrite ( $IPConfig .IPAddress ( $i ) & @CRLF ) Next EndIf Next
转载于:https://www.cnblogs.com/darkdance/archive/2011/11/29/2267058.html