Powershell小技巧之使用WMI工具
WMI是一個(gè)強(qiáng)大的技術(shù):只需要簡(jiǎn)單的指定一個(gè)WMI類名就能返回它類的所有實(shí)例:
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
}
設(shè)置搜索條件后,代碼將搜索出包含指定屬性名的類(還可以通過(guò)通配符擴(kuò)大搜索范圍)
下面將找出所有包含“resolution”結(jié)尾的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...
接著,就可以使用類名查看它的有效數(shù)據(jù)啦:
PS> Get-WmiObject -Class CIM_CacheMemory | Select-Object -Property *
心得:
其實(shí)有個(gè)類似功能的圖形工具比這段代碼更方便更直觀,它叫ScriptomaticV2。
支持Powershell所有版本
相關(guān)文章
PowerShell入門教程之創(chuàng)建和使用配置文件實(shí)例
這篇文章主要介紹了PowerShell入門教程之創(chuàng)建和使用配置文件實(shí)例,PowerShell的配置文件都是些普通的PowerShell腳本文件,需要的朋友可以參考下2014-10-10Windows Powershell擴(kuò)展類型系統(tǒng)
本文主要詳細(xì)介紹了集中將對(duì)象轉(zhuǎn)換成文本的方法,并附上示例說(shuō)明,非常的實(shí)用,有需要的朋友可以參考下2014-09-09PowerShell函數(shù)參數(shù)設(shè)置成自動(dòng)識(shí)別數(shù)據(jù)類型的例子
這篇文章主要介紹了PowerShell函數(shù)參數(shù)設(shè)置成自動(dòng)識(shí)別數(shù)據(jù)類型的例子,這個(gè)方法可以很方便的使用參數(shù)集,而不用每次指定名稱,需要的朋友可以參考下2014-07-07Powershell腳本的4種執(zhí)行權(quán)限介紹
這篇文章主要介紹了Powershell腳本的4種執(zhí)行權(quán)限介紹,Windows默認(rèn)不允許任何腳本運(yùn)行,你可以使用"Set-ExecutionPolicy"cmdlet來(lái)改變的你PowerShell環(huán)境,共有4種運(yùn)行權(quán)限,需要的朋友可以參考下2015-06-06PowerShell中使用Get-ChildItem命令讀取目錄、文件列表使用例子和小技巧
這篇文章主要介紹了PowerShell中使用Get-ChildItem命令讀取目錄、文件列表使用例子和小技巧,比如只讀取目錄列表的方法,只讀取文件列表的方法,需要的朋友可以參考下2014-08-08Windows Powershell 通過(guò)函數(shù)擴(kuò)展別名
這篇文章主要介紹了Windows Powershell 通過(guò)函數(shù)擴(kuò)展別名,需要的朋友可以參考下2014-09-09Powershell小技巧之使用Update-TypeData擴(kuò)展類型系統(tǒng)
昨天演示獲取時(shí)辰《PowerShell獲取當(dāng)前的時(shí)辰 》,并不只是為了獲取時(shí)辰,還為了使用Update-TypeData命令將時(shí)辰這一屬性,牢牢地幫到DateTime類型上。2014-09-09PowerShell小技巧之使用New-Module命令動(dòng)態(tài)創(chuàng)建對(duì)象
這篇文章主要介紹了在PowerShell中使用New-Module命令動(dòng)態(tài)創(chuàng)建對(duì)象,比New-Object高大上很多了吧2014-09-09