当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Get-CimInstance 获取 WMI 对象

作者:精品下载站 日期:2024-12-14 03:03:12 浏览:14 分类:玩电脑

使用 Get-CimInstance 获取 WMI 对象


该示例仅适用于Windows平台。

Windows Management Instrumentation (WMI) 是 Windows 系统管理的核心技术,因为它以统一的方式公开广泛的信息。由于 WMI 的功能非常强大,用于访问 WMI 对象的 PowerShell cmdlet Get-CimInstance 是进行实际工作最有用的命令之一。我们将讨论如何使用 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 参数。我们展示它是为了演示最一般的情况并提醒您有关参数的信息。 命名空间默认为root/CIMV2,也可以省略。最后,大多数 cmdlet 允许您省略常用参数的名称。使用 Get-CimInstance 时,如果没有为第一个参数指定名称,PowerShell 会将其视为 Class 参数。这意味着最后一个命令可以通过键入以下命令发出:

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

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯