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存储库
”的信息。
(3)、WMI 有一个 Service
WMI有一个一直运行的Windows服务,名称为Winmgmt
,可以响应用户的访问。
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\cimv2Name 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_ServiceName 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 *_processorNameSpace:ROOT/cimv2CimClassName 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_ProcessorDeviceID 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 CmdletCommandType 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 CimCmdletsCommandType 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:
PowerShell中使用WMI或CIM相关推荐
- WMI与CIM的区别
文章目录 定义 WMI的历史 微软打算弃用WMI 开放管理基础设施(OMI) 定义 WMI(Windows Management Instrumentation)作为Windows操作系统的对外的管理 ...
- PowerShell_5_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念
据我个人所知,windows下的cmd shell除了能够通过修改系统参数来对其中的环境变量进行改变外,其环境的可自定义性相对来说很困难,而在Linux环境中,可以通过修改/etc目录下的某些配置文件 ...
- 在 PowerShell 中使用 SQL Server (1)
一.概述 SQL Server 2012 提供了一个名为 sqlps 的 Windows PowerShell 模块,该模块用于将 SQL Server 组件导入到 Windows PowerShel ...
- .NET 框架中的 WMI 命名空间
.NET 框架中的 WMI 命名空间 .NET框架中与WMI规范有关的命名空间有两个,分别是System.Management和System.Management.Instrumentation两 ...
- linux svn missingargument -m,缺少參數-m在windows powershell中使用svn。
I'm trying to run this at Windows PowerShell: 我試着在Windows PowerShell中運行它: svn ci -m "" dir ...
- 如何在PowerShell中使用带有空格和引号的参数运行EXE文件
本文翻译自:How to run an EXE file in PowerShell with parameters with spaces and quotes How do you run the ...
- (实用)将wordpad添加到Windows PowerShell中
PowerShell能够直接打开notepad,但是无法调用wordpad,因为后者的可执行文件并不在系统默认的环境变量$env:Path中,只要将wordpad所在的路径添加到$env:Path,就 ...
- 在PowerShell中创建对象并添加属性成员
可在PowerShell中创建对象: 使用 New-Object 创建一个对象,名为 $box:对象初创时是空的,类型为System.Object: 如果直接为$box的一个属性赋值,该属性尚未创建, ...
- powershell提取html字段,Windows PowerShell:PowerShell 中的 HTML 报告
Windows PowerShell:PowerShell 中的 HTML 报告 08/17/2016 本文内容 事实上,您可以使用 Microsoft Windows PowerShell Cmdl ...
- 使用管道符在PowerShell中进行各种数据操作
最近在培训PowerShell,在讲到Pipeline的时候,对于我这种长期和数据(数据库)打交道的人来说,觉得很实用,所以写此博文,记录一下. 无论是在Linux中写Bash脚本还是在Window上 ...
最新文章
- c语言设备管理系统实训答辩,C语言设计(力学实验设备管理系统)1答辩.doc
- go语言基础到提高(12)-函数类型与函数变量
- Nvelocity 模板引擎 实例
- java 树形图构建
- 分布式模块之间的调用_分布式事务
- 41.字符串函数(三)
- 使用js生成条形码以及二维码
- 4. PHP 操作 memcache(1)
- wps打开pdf乱码_wps打开pdf乱码
- java自行车销售系统_基于 javaee 自行车租赁系统,源码分享
- oracle价格的数据类型,oracle数据库中的number类型
- tp6/thinkphp6实现微信小程序支付/微信支付v3版本,全部流程代码
- 搭建以太坊私有链多节点环境
- 开启愉快的路由之旅:广工校园网Wifi
- piblic class 和class的区别
- 尼日利亚4g频段_4G全球频段划分及主要运营商对应表2015版.xls
- 将数组以逗号拼接_javascript将数组拼接成一个字符串
- ansible模块管理与主机清单配置
- 方向导数、梯度、等高线、数量场与向量场
- RK3399 点亮mipi屏和EDP屏