天天看點

PowerShell中使用WMI或CIM

PowerShell使用WMI或CIM

文章目錄

    • PowerShell使用WMI或CIM
      • 一、什麼是WMI 或CIM?
        • 1.1 什麼是CIM?
        • 1.2 什麼是WMI?
        • 1.3 WMI和CIM的差別
      • 二、如何使用WMI或CIM?
        • 2.1、使用 Get-WmiObject
          • 2.1.1 查詢 WMI 或 CIM 類名稱
          • 2.1.2 使用 WMI 或 CIM 類
          • 2.1.3 小結
        • 2.2、使用 Get-CimInstance
          • 2.2.1 查詢 CIM 或 WMI 類名稱
          • 2.2.2 使用 CIM 或 WMI 類
          • 2.2.3 小結

一、什麼是WMI 或CIM?

1.1 什麼是CIM?

​ 通用資訊模型(

Common Information Model

,簡稱

“CIM”

),是由DMTF制定的用于管理計算機系統和網絡的工業規範,它為系統、網絡、應用程式及服務的管理資訊提供了統一的定義,并且允許廠商基于此進行擴充。

​ CIM 的概念适用于所有領域的管理,而且所有的領域可以獨立的進行技術實施,由公共資訊模型開發出的管理軟體可以統一異構環境下的産品管理。CIM 由以下幾部分組成 :

  • CIM 規範- 定義了整合管理模型的語言和方法論。
  • CIM 标準模型 - 定義了對計算機系統,應用程式,網絡和裝置描述資訊模型。 CIM 标準模型由以下及部分組成:
    • 核心模型 - 提供了基本的受管對象的假設資訊,内容包括一系列描述和分析受管系統的類和他們之間的關聯資訊。
    • 公共模型 - 實作了管理領域的公共概念。這些概念獨立于各個特殊的技術和實作。可以說這個模型提供了一個管理軟體發展的基礎。
    • 拓展模型 - 他們是在公共基礎上拓展一些與平台相關的模型,比如作業系統的模型等。一些大公司如 IBM 公司都有根據自己的産品所拓展出一系列的為自己産品服務的模型。

1.2 什麼是WMI?

​ WMI是英文“Windows Management Instrumentation”的縮寫,翻譯過來是***Windows管理規範***。

通俗的講,WMI是一個技術或者規範,微軟根據它開發出了一系列的東西。

​ 主要有以下内容:

(1)、WMI 有一組 API

​ WMI有一組對外暴露的API,可供其他語言,如

C#、VBScript

PowerShell

來調用。

(2)、WMI 有一個存儲庫

​ 盡管WMI的多數執行個體資料都不存儲在WMI中,但是WMI确實有一個存儲庫,用來存放提供程式提供的類資訊,或者稱為類的藍圖或者Schema,Windows系統可以使用“

wmimgmt.msc

”可以查詢一些“

WMI存儲庫

”的資訊。

PowerShell中使用WMI或CIM

(3)、WMI 有一個 Service

​ WMI有一個一直運作的Windows服務,名稱為

Winmgmt

,可以響應使用者的通路。

PowerShell中使用WMI或CIM

1.3 WMI和CIM的差別

​ WMI是一種在 Windows 上實作 CIM 标準的 CIM 伺服器。通俗的講:

  • CIM協定

    通用協定

    WMI協定

    是Windows的

    專用協定

  • Get-WmiObject

    cmdlet隻能用于

    Windows系統

    中,

    Get-CimInstance

    既可以用于

    Windows系統

    ,也可以用于其他

    Linux系

    MacOS

    系統中。
  • 在Windows系統中,可以認為CIM等同于WMI

二、如何使用WMI或CIM?

​ 在Windows系統中,PowerShell有兩個Cmdlet調用WMI或CIM,分别為

Get-WmiObject

Get-CimInstance

。這兩個cmdlet可以結合"

-class <WMI-Class>

"以及一些其他參數進行使用。

​ ***注:WMI cmdlet 已棄用 ,建議使用 ”

Get-CimInstance

“替代 ”

Get-WmiObject

***“

​ 下面來講解這兩個

cmdlet

的用法:

2.1、使用 Get-WmiObject

​ Get-WmiObject的文法結構:

Get-WmiObject 
[[-Class] <System.String>]
[[-Property] <System.String[]>] 
[-Amended] 
[-AsJob] 
[-Authentication {Default | None | Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}] 
[-Authority <System.String>] 
[-ComputerName <System.String[]>] 
[-Credential <System.Management.Automation.PSCredential>] 
[-DirectRead] 
[-EnableAllPrivileges] 
[-Filter <System.String>] 
[-Impersonation {Default | Anonymous | Identify | Impersonate | Delegate}] 
[-Locale <System.String>] 
[-Namespace <System.String>] 
[-ThrottleLimit <System.Int32>] 
[<CommonParameters>]
           
Get-WmiObject 
[[-Class] <System.String>] 
[-Amended] 
[-AsJob] 
[-Authentication {Default | None | Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}] 
[-Authority <System.String>] 
[-ComputerName <System.String[]>] 
[-Credential <System.Management.Automation.PSCredential>] 
[-EnableAllPrivileges] 
[-Impersonation {Default | Anonymous | Identify | Impersonate | Delegate}] 
[-List] 
[-Locale <System.String>] 
[-Namespace <System.String>] 
[-Recurse] 
[-ThrottleLimit <System.Int32>] 
[<CommonParameters>]
           
Get-WmiObject 
[-Amended] 
[-AsJob] 
[-Authentication {Default | None | Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}] 
[-Authority <System.String>] 
[-ComputerName <System.String[]>] 
[-Credential <System.Management.Automation.PSCredential>] 
[-DirectRead] 
[-EnableAllPrivileges] 
[-Impersonation {Default | Anonymous | Identify | Impersonate | Delegate}] 
[-Locale <System.String>] 
[-Namespace <System.String>] 
-Query <System.String> 
[-ThrottleLimit <System.Int32>] 
[<CommonParameters>]
           
2.1.1 查詢 WMI 或 CIM 類名稱

​ 使用

Get-WmiObject -List

可以列出所有的 WMI對象的類,後接名稱可以過濾查詢,支援通配符。

​ 查詢:

Get-WmiObject -List *_processor | Format-Table -Wrap

NameSpace:ROOT\cimv2

Name                                Methods              Properties
----                                -------              ----------
CIM_Processor                       {SetPowerState, Rese {AddressWidth, Availability, Caption, ConfigManagerErrorCode...
                                    t}                   }
Win32_Processor                     {SetPowerState, Rese {AddressWidth, Architecture, AssetTag, Availability...}
                                    t}
Win32_PerfFormattedData_PerfOS_Proc {}                   {C1TransitionsPersec, C2TransitionsPersec, C3TransitionsPersec,
essor                                                     Caption...}
Win32_PerfRawData_PerfOS_Processor  {}                   {C1TransitionsPersec, C2TransitionsPersec, C3TransitionsPersec,
                                                          Caption...}
           
2.1.2 使用 WMI 或 CIM 類

​ 使用

Get-WmiObject -Class <WMI-Class>

可以查詢指定的 WMI類對象,其中

-Class

可以省略。

​ 查詢主機CPU資訊:

Get-WmiObject win32_processor

Caption           : Intel64 Family 6 Model 158 Stepping 13
DeviceID          : CPU0
Manufacturer      : GenuineIntel
MaxClockSpeed     : 3000
Name              : Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
SocketDesignation : U3E1
           

​ WMI類會自帶一些方法和屬性,上面列出的

:

左邊的就是屬性,右側的是屬性的值。

​ 可以通過"

.<PropertyName>

"擷取屬性的值,通過”

.<MethodName>()

“調用WMI對象的方法。

  • 擷取WMI對象的屬性值

(Get-WmiObject win32_processor).Name

PS C:\> (Get-WmiObject win32_processor).Name
Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
           
  • 引用WMI對象的方法
    • 查詢WMI對象的方法

      可以配合

      Get-Member

      查詢WMI對象的使用方法。

      PS C:> Get-WmiObject -Class Win32_Service | where {$_.Name -eq ‘winrm’}

      ExitCode : 0

      Name : WinRM

      ProcessId : 0

      StartMode : Auto

      State : Stopped

      Status : OK

      上面指令執行後,查到了一個對象(WinRM服務對象,目前為關閉狀态),将其通過管道(|)傳遞給Get-Member 可以查詢此對象的方法和屬性
      (Get-WmiObject -Class Win32_Service | where {$_.Name -eq ‘winrm’}) | Get-Member -MemberType Method
      TypeName:System.Management.ManagementObject#root\cimv2\Win32_Service
      
      Name                  MemberType Definition
      ----                  ---------- ----------
      Change                Method     System.Management.ManagementBaseObject Change(System.String DisplayName, System.Stri...
      ChangeStartMode       Method     System.Management.ManagementBaseObject ChangeStartMode(System.String StartMode)
      Delete                Method     System.Management.ManagementBaseObject Delete()
      GetSecurityDescriptor Method     System.Management.ManagementBaseObject GetSecurityDescriptor()
      InterrogateService    Method     System.Management.ManagementBaseObject InterrogateService()
      PauseService          Method     System.Management.ManagementBaseObject PauseService()
      ResumeService         Method     System.Management.ManagementBaseObject ResumeService()
      SetSecurityDescriptor Method     System.Management.ManagementBaseObject SetSecurityDescriptor(System.Management.Manag...
      StartService          Method     System.Management.ManagementBaseObject StartService()
      StopService           Method     System.Management.ManagementBaseObject StopService()
      UserControlService    Method     System.Management.ManagementBaseObject UserControlService(System.Byte ControlCode)
                 
    • 引用WMI對象的方法

      根據查詢結果可以看到,有”StartService“方法,可以直接調用此方法,啟動WinRM服務。

      (Get-WmiObject -Class Win32_Service | where {$_.Name -eq ‘winrm’}).StartService()
      PS C:\> (Get-WmiObject -Class Win32_Service | where {$_.Name -eq 'winrm'}).StartService()
      
      
      __GENUS          : 2
      __CLASS          : __PARAMETERS
      __SUPERCLASS     :
      __DYNASTY        : __PARAMETERS
      __RELPATH        :
      __PROPERTY_COUNT : 1
      __DERIVATION     : {}
      __SERVER         :
      __NAMESPACE      :
      __PATH           :
      ReturnValue      : 2
      PSComputerName   :
                 
      結果驗證
      Get-WmiObject -Class Win32_Service | where {$_.Name -eq ‘winrm’}
      PS C:\> Get-WmiObject -Class Win32_Service | where {$_.Name -eq 'winrm'}
      
      
      ExitCode  : 0
      Name      : WinRM
      ProcessId : 24460
      StartMode : Auto
      State     : Running
      Status    : OK
                 
      注意:開啟或關閉服務,需要管理者權限的Powershell執行。
2.1.3 小結

​ 使用”

Get-WmiObject -List <ClassName>

“查詢WMI類名稱,其中“”支援使用通配符。使用”

Get-WmiObject <ClassName>

“直接使用WMI類,同時可以配合**

Get-Member

**查詢對象的方法和屬性。

2.2、使用 Get-CimInstance

​ PowerShell 版本 3.0 中引入了 通用資訊模型 (CIM) cmdlet。CIM cmdlet 的設計目的是使其可以同時在 Windows 和非 Windows 計算機上使用。

​ 由于 WMI cmdlet 已棄用,是以建議使用 CIM cmdlet 代替 WMI cmdlet。

2.2.1 查詢 CIM 或 WMI 類名稱

​ 可以通過**

Get-CimClass

**指令查詢CIM類的名稱,支援通配符,

-Class

可省略。

Get-CimClass *_processor
PS C:\> Get-CimClass *_processor


   NameSpace:ROOT/cimv2

CimClassName                        CimClassMethods      CimClassProperties
------------                        ---------------      ------------------
CIM_Processor                       {SetPowerState, R... {Caption, Description, InstallDate, Name...}
Win32_Processor                     {SetPowerState, R... {Caption, Description, InstallDate, Name...}
Win32_PerfFormattedData_PerfOS_P... {}                   {Caption, Description, Name, Frequency_Object...}
Win32_PerfRawData_PerfOS_Processor  {}                   {Caption, Description, Name, Frequency_Object...}
           
2.2.2 使用 CIM 或 WMI 類

​ 使用CIM類是用的

Get-CimInstance

指令。使用方法和”

Get-WmiObject

“類似,隻是顯示的東西比”

Get-WmiObject

“更少。

Get-CimInstance -Class Win32_Processor
PS C:\> Get-CimInstance -Class Win32_Processor

DeviceID Name                                    Caption                                MaxClockSpeed SocketDesignation
-------- ----                                    -------                                ------------- -----------------
CPU0     Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz Intel64 Family 6 Model 158 Stepping 13 3000          U3E1
           
2.2.3 小結

​ ”

Get-CimInstance

“和”

Get-WmiObject

“使用方式類似。主要是”

Get-WmiObject

“隻能

Windows

系統中用,”

Get-CimInstance

“支援其他系統。

附錄 1: 其他 WMI指令

PS C:\> Get-Command *WMI* -CommandType Cmdlet

CommandType     Name                Version    Source
-----------     ----                -------    ------
Cmdlet          Get-WmiObject       3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Invoke-WmiMethod    3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Register-WmiEvent   3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Remove-WmiObject    3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Set-WmiInstance     3.1.0.0    Microsoft.PowerShell.Management
           

附錄 2: 其他 CIM指令

PS C:\> Get-Command -Module CimCmdlets

CommandType     Name                             Version    Source
-----------     ----                             -------    ------
Cmdlet          Export-BinaryMiLog               1.0.0.0    CimCmdlets
Cmdlet          Get-CimAssociatedInstance        1.0.0.0    CimCmdlets
Cmdlet          Get-CimClass                     1.0.0.0    CimCmdlets
Cmdlet          Get-CimInstance                  1.0.0.0    CimCmdlets
Cmdlet          Get-CimSession                   1.0.0.0    CimCmdlets
Cmdlet          Import-BinaryMiLog               1.0.0.0    CimCmdlets
Cmdlet          Invoke-CimMethod                 1.0.0.0    CimCmdlets
Cmdlet          New-CimInstance                  1.0.0.0    CimCmdlets
Cmdlet          New-CimSession                   1.0.0.0    CimCmdlets
Cmdlet          New-CimSessionOption             1.0.0.0    CimCmdlets
Cmdlet          Register-CimIndicationEvent      1.0.0.0    CimCmdlets
Cmdlet          Remove-CimInstance               1.0.0.0    CimCmdlets
Cmdlet          Remove-CimSession                1.0.0.0    CimCmdlets
Cmdlet          Set-CimInstance                  1.0.0.0    CimCmdlets
           

參考:

1.WMI入門(一):什麼是WMI

2.使用WMI: