使用 Get-CimInstance 获取 WMI 对象

此示例仅适用于 Windows 平台。

Windows Management Instrumentation(WMI)是 Windows 系统管理的核心技术,因为它以统一的方式公开各种信息。 由于 WMI 的强大功能,访问 WMI 对象的 PowerShell cmdlet Get-CimInstance是完成实际工作最有用的 cmdlet 之一。 我们将讨论如何使用 CIM cmdlet 访问 WMI 对象,以及如何使用 WMI 对象执行特定作。

列出 WMI 类

大多数 WMI 用户面临的第一个问题是尝试了解 WMI 可以执行的操作。 WMI 类描述可管理的资源。 有数百个 WMI 类,其中有些包含数十个属性。

Get-CimClass 通过使 WMI 可发现来解决此问题。 可以通过键入以下内容获取本地计算机上可用的 WMI 类的列表:

Get-CimClass -Namespace root/CIMV2 | 
    Where-Object CimClassName -Like Win32* | 
    Select-Object CimClassName
CimClassName
------------
Win32_DeviceChangeEvent
Win32_SystemConfigurationChangeEvent
Win32_VolumeChangeEvent
Win32_SystemTrace
Win32_ProcessTrace
Win32_ProcessStartTrace
Win32_ProcessStopTrace
Win32_ThreadTrace
Win32_ThreadStartTrace
Win32_ThreadStopTrace
...

可以使用 ComputerName 参数从远程计算机检索相同的信息,并指定计算机名称或 IP 地址:

Get-CimClass -Namespace root/CIMV2 -ComputerName 192.168.1.29

远程计算机返回的类列表可能因计算机正在运行的特定作系统而有所不同,并且已安装的应用程序会添加特定的 WMI 扩展。

注释

使用 CIM cmdlet 连接到远程计算机时,远程计算机必须运行 WMI,并且所使用的帐户必须位于远程计算机上的本地 管理员 组中。 远程系统不需要安装 PowerShell。 这可以让你管理未运行 PowerShell 但具有 WMI 的操作系统。

显示 WMI 类详细信息

如果已知道 WMI 类的名称,则可以使用它立即获取信息。 例如,WMI 中用于检索有关计算机的信息的类之一是 Win32_OperatingSystem

Get-CimInstance -Class Win32_OperatingSystem
SystemDirectory     Organization BuildNumber RegisteredUser SerialNumber            Version
---------------     ------------ ----------- -------------- ------------            -------
C:\WINDOWS\system32 Microsoft    22621       USER1          00330-80000-00000-AA175 10.0.22621

虽然我们显示了所有参数,但命令可以更简洁的方式表示。 连接到本地系统时,不需要 ComputerName 参数。 我们展示它是为了说明最常见的情况,并提醒你注意参数。 命名空间 默认为 根/CIMV2,也可以省略。 最后,大多数 cmdlet 都允许省略通用参数的名称。 对于 Get-CimInstance,如果未为第一个参数指定名称,PowerShell 会将它视为 参数。 这意味着最后一个命令可以通过键入以下内容发出:

Get-CimInstance Win32_OperatingSystem

Win32_OperatingSystem 类的属性比此处显示的属性多得多。 可以使用 Get-Member 查看所有属性。 WMI 类的属性与其他对象属性一样自动可用:

Get-CimInstance -Class Win32_OperatingSystem | Get-Member -MemberType Property
   TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_OperatingSystem
Name                                      MemberType Definition
----                                      ---------- ----------
BootDevice                                Property   string BootDevice {get;}
BuildNumber                               Property   string BuildNumber {get;}
BuildType                                 Property   string BuildType {get;}
Caption                                   Property   string Caption {get;}
CodeSet                                   Property   string CodeSet {get;}
CountryCode                               Property   string CountryCode {get;}
CreationClassName                         Property   string CreationClassName {get;}
CSCreationClassName                       Property   string CSCreationClassName {get;}
CSDVersion                                Property   string CSDVersion {get;}
CSName                                    Property   string CSName {get;}
CurrentTimeZone                           Property   int16 CurrentTimeZone {get;}
DataExecutionPrevention_32BitApplications Property   bool DataExecutionPrevention_32BitApplications {get;}
DataExecutionPrevention_Available         Property   bool DataExecutionPrevention_Available {get;}
...

使用 Format cmdlet 显示非默认属性

如果希望默认情况下未显示 Win32_OperatingSystem 类中包含的信息,可以使用 Format cmdlet 来显示它。 例如,如果要显示可用的内存数据,请键入:

Get-CimInstance -Class Win32_OperatingSystem | Format-Table -Property TotalVirtualMemorySize, TotalVisibleMemorySize, FreePhysicalMemory, FreeVirtualMemory, FreeSpaceInPagingFiles
TotalVirtualMemorySize TotalVisibleMemorySize FreePhysicalMemory FreeVirtualMemory FreeSpaceInPagingFiles
---------------------- ---------------------- ------------------ ----------------- ----------------------
              41787920               16622096            9537952          33071884               25056628

注释

通配符可以与 Format-Table中的属性名称一起使用,因此最终的管道元素可以简化为 Format-Table -Property Total*Memory*, Free*

如果通过键入以下内容将内存数据格式化为列表,则内存数据可能更具可读性:

Get-CimInstance -Class Win32_OperatingSystem | Format-List Total*Memory*, Free*
TotalVirtualMemorySize : 41787920
TotalVisibleMemorySize : 16622096
FreePhysicalMemory     : 9365296
FreeSpaceInPagingFiles : 25042952
FreeVirtualMemory      : 33013484
Name                   : Microsoft Windows 11 Pro|C:\Windows|\Device\Harddisk0\Partition2