Powershell中使用WMI工具例子

 更新时间:2014年11月07日 10:32:26   投稿:junjie  
这篇文章主要介绍了Powershell中使用WMI工具例子,本文先是讲解了列出WMI类的方法,然后根据需要使用相应的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 *

相关文章

  • PowerShell脚本性能优化技巧总结

    PowerShell脚本性能优化技巧总结

    这篇文章主要介绍了PowerShell脚本性能优化技巧总结,一些PowerShell脚本可能很容易消耗很多内存,或者运行太多时间,甚至兼而有之,本文会分享几个PowerShell小技巧来提高这一类脚本的性能,需要的朋友可以参考下
    2014-05-05
  • PowerShell管理Win Server 2008 R2

    PowerShell管理Win Server 2008 R2

    在Windows Serve 2008 R2中,一个重要改进就是PowerShell版本升级为2.0。Win 2008 R2包括一系列新的服务器管理界面,这些均建立在PowerShell 2.0之上。它新增了240个cmdlets命令集,新的PowerShell图形用户界面也增添了开发功能,从而用户能更简单创建自己的命令行。
    2015-09-09
  • PowerShell查看本机文件关联程序和默认打开程序的方法

    PowerShell查看本机文件关联程序和默认打开程序的方法

    这篇文章主要介绍了PowerShell查看本机文件关联程序和默认打开程序的方法,本文给出了查看方法,同时给出了一份读取结果,需要的朋友可以参考下
    2015-06-06
  • powershell常用命令大全

    powershell常用命令大全

    PowerShell是强大的自动化工具,涵盖服务管理、事件日志查询、变量操作、脚本执行、远程管理以及模块管理等,本文介绍powershell常用命令大全,感兴趣的朋友一起看看吧
    2025-02-02
  • powershell远程管理服务器磁盘空间的实现代码

    powershell远程管理服务器磁盘空间的实现代码

    这篇文章主要介绍了powershell远程管理服务器磁盘空间的实现代码,需要的朋友可以参考下
    2016-11-11
  • PowerShell中执行Javascript的方法示例

    PowerShell中执行Javascript的方法示例

    这篇文章主要介绍了PowerShell中执行Javascript的方法示例,特殊场景下可能会用到这个技巧,需要的朋友可以参考下
    2014-07-07
  • PowerShell操作Excel、CSV详细介绍

    PowerShell操作Excel、CSV详细介绍

    这篇文章主要介绍了PowerShell操作Excel、CSV详解,本文比较深入的探讨了PowerShell中如何操作Excel及CSV,需要的朋友可以参考下
    2015-01-01
  • Powershell实现捕获系统内置EXE程序的异常

    Powershell实现捕获系统内置EXE程序的异常

    这篇文章主要介绍了Powershell实现捕获系统内置EXE程序的异常,系统内置的EXE程序是指如robocopy.exe、ipconfig.exe等命令的实现程序,需要的朋友可以参考下
    2014-12-12
  • PowerShell查找数组内容、搜索数组、查询数组的方法

    PowerShell查找数组内容、搜索数组、查询数组的方法

    这篇文章主要介绍了PowerShell查找数组内容、搜索数组、查询数组的方法,使用PowerShell中的一些操作符来实现搜索功能,需要的朋友可以参考下
    2014-08-08
  • PowerShell设置文件只读、隐藏属性的方法

    PowerShell设置文件只读、隐藏属性的方法

    这篇文章主要介绍了PowerShell设置文件只读、隐藏属性的方法,包括系统、存档和无内容索引属性都有介绍,需要的朋友可以参考下
    2014-08-08

最新评论