使用WMI或CIM

使用WMI或CIM

1、什麼是WMI ?

WMI是英文「Windows Management Instrumentation」的縮寫,翻譯過來是Windows管理規範html

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

主要有如下內容:ide

一、WMI 有一組 APIui

WMI有一組對外暴露的API,可供其餘語言,如C#、VBScriptPowerShell來調用。spa

二、WMI 有一個存儲庫翻譯

儘管WMI的多數實例數據都不存儲在WMI中,可是WMI確實有一個存儲庫,用來存放提供程序提供的類信息,或者稱爲類的藍圖或者Schema設計

三、WMI 有一個 Servicecode

WMI有一個一直運行的Windows服務,名稱爲Winmgmt,能夠響應用戶的訪問。orm

2、如何使用WMI?

PowerShell有兩個Cmdlet,分別爲Get-WinObjectGet-CimInstancehtm

這兩個cmdlet能夠結合"-class <WMI-Class>"以及一些其餘參數進行使用。

WMI cmdlet 已棄用 ,建議使用Get-CimInstance替代Get-WinObject

2.一、使用 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 類名稱

使用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 類

使用Get-WmiObject -Class &lt;WMI-Class&gt; 能夠查詢指定的 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類會自帶一些方法和屬性,上面列出的:左邊的就是屬性,右側的是屬性的值。

能夠經過".&lt;PropertyName&gt;"獲取屬性的值,經過」.&lt;MethodName&gt;()「調用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 Win32Service | where {$.Name -eq 'winrm'}

    ExitCode : 0
    Name : WinRM
    ProcessId : 0
    StartMode : Auto
    State : Stopped
    Status : OK

    上面命令執行後,查到了一個對象(WinRM服務對象,當前爲關閉狀態),將其經過管道(|)傳遞給Get-Member 能夠查詢此對象的方法和屬性

    (Get-WmiObject -Class Win32Service | 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 Win32Service | 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 Win32Service | 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 &lt;ClassName&gt;「查詢WMI類名稱,使用」Get-WmiObject &lt;ClassName&gt;「直接使用WMI類,同時配合Get-Member查詢對象的方法和屬性。

2.二、使用 Get-CimInstance

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

​ 因爲 WMI cmdlet 已棄用,所以建議使用 CIM cmdlet 代替 WMI cmdlet。

2.2.1 查詢 CIM 類名稱

​ 能夠經過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 類

使用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「使用方式相似,只是有一個專門的命令能夠查詢WMI或CIM類的信息。

附錄 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 https://www.cnblogs.com/ceachy/archive/2013/03/21/WMI_What.html

2.使用WMI:

https://docs.microsoft.com/zh-cn/powershell/scripting/learn/ps101/07-working-with-wmi

目錄:返回個人PowerShell筆記

相關文章
相關標籤/搜索