Powershell小技巧之使用WMI工具
wmi是一个强大的技术:只需要简单的指定一个wmi类名就能返回它类的所有实例:
ps> get-wmiobject -class win32_bios
smbiosbiosversion : 76cn27ww
manufacturer : lenovo
name : 76cn27ww
serialnumber : 1006250300406
version : lenovo - 1
你如何知道它有哪些类呢?这里有一款查找工具:
function find-wmiclass
{
param
(
[parameter(mandatory=$true)]
$searchterm = 'resolution'
)
get-wmiobject -class * -list |
where-object { $_.properties.count -ge 3 } |
where-object { $_.name -notlike 'win32_perf*' } |
where-object {
$listofnames = $_.properties | select-object -expandproperty name
($listofnames -like "*$searchterm*") -ne $null
} |
sort-object -property name
}
设置搜索条件后,代码将搜索出包含指定属性名的类(还可以通过通配符扩大搜索范围)
下面将找出所有包含“resolution”结尾的wmi类:
ps> find-wmiclass -searchterm *resolution
namespace: root\cimv2
name methods properties
---- ------- ----------
cim_cachememory {setpowerstate, r... {access, additionalerr...
cim_currentsensor {setpowerstate, r... {accuracy, availabilit...
cim_flatpanel {setpowerstate, r... {availability, caption...
cim_memory {setpowerstate, r... {access, additionalerr...
cim_monitorresolution {} {caption, description,...
cim_nonvolatilestorage {setpowerstate, r... {access, additionalerr...
cim_numericsensor {setpowerstate, r... {accuracy, availabilit...
cim_pcvideocontroller {setpowerstate, r... {acceleratorcapabiliti...
cim_pointingdevice {setpowerstate, r... {availability, caption...
cim_printer {setpowerstate, r... {availability, availab...
cim_tachometer {setpowerstate, r... {accuracy, availabilit...
cim_temperaturesensor {setpowerstate, r... {accuracy, availabilit...
cim_videocontroller {setpowerstate, r... {acceleratorcapabiliti...
cim_videocontrollerresolution {} {caption, description,...
cim_volatilestorage {setpowerstate, r... {access, additionalerr...
cim_voltagesensor {setpowerstate, r... {accuracy, availabilit...
win32_cachememory {setpowerstate, r... {access, additionalerr...
win32_currentprobe {setpowerstate, r... {accuracy, availabilit...
win32_displaycontrollerconfigura... {} {bitsperpixel, caption...
win32_memoryarray {setpowerstate, r... {access, additionalerr...
win32_memorydevice {setpowerstate, r... {access, additionalerr...
win32_networkadapterconfiguration {enabledhcp, rene... {arpalwayssourceroute,...
win32_pointingdevice {setpowerstate, r... {availability, caption...
win32_printer {setpowerstate, r... {attributes, availabil...
win32_printerconfiguration {} {bitsperpel, caption, ...
win32_smbiosmemory {setpowerstate, r... {access, additionalerr...
win32_temperatureprobe {setpowerstate, r... {accuracy, availabilit...
win32_videoconfiguration {} {actualcolorresolution...
win32_videocontroller {setpowerstate, r... {acceleratorcapabiliti...
win32_voltageprobe {setpowerstate, r... {accuracy, availabilit...
接着,就可以使用类名查看它的有效数据啦:
ps> get-wmiobject -class cim_cachememory | select-object -property *
心得:
其实有个类似功能的图形工具比这段代码更方便更直观,它叫scriptomaticv2。
支持powershell所有版本
推荐阅读
-
iOS开发之topLayoutGuide和bottomLayoutGuide的使用小技巧分享
-
iOS开发之topLayoutGuide和bottomLayoutGuide的使用小技巧分享
-
Yii2使用小技巧之通过 Composer 添加 FontAwesome 字体资源
-
PHP小技巧之JS和CSS优化工具Minify的使用方法
-
Git使用小技巧之回滚与撤销详解
-
PowerShell中使用Get-ChildItem命令读取目录、文件列表使用例子和小技巧
-
Yii2使用小技巧之通过 Composer 添加 FontAwesome 字体资源
-
PHP小技巧之JS和CSS优化工具Minify的使用方法
-
PS移动工具使用小技巧你知道吗
-
cdr工具使用的两个小技巧