zoukankan      html  css  js  c++  java
  • 2019-8-31-PowerShell-使用-WMI-获取信息

    title author date CreateTime categories
    PowerShell 使用 WMI 获取信息
    lindexi
    2019-08-31 16:55:58 +0800
    2019-2-22 21:0:51 +0800
    PowerShell WMI

    在 PowerShell 可以很容易使用 WMI 拿到系统的信息,如果有关注我的网站,就会发现我写了很多通过 WMI 拿到系统的显卡,系统安装的软件等方法,本文告诉大家如果通过 PowerShell 拿到 WMI 类里面的属性

    在 Windows 系统通过 Windows Management Instrumentation (WMI) 统一管理系统的配置,在 PowerShell 能使用 WMI 的功能进行获取系统

    很少有人知道 WMI 里面包含了多少可以使用的类,包括我之前写的很多博客,实际上也只是里面的很少,通过下面的例子告诉大家如何获取设备里面包含的类

    获取 WMI 类

    在使用 WMI 之前需要知道 WMI 是能做什么的,这个方法能做的就是描述系统能被管理的资源,在系统里面包含了几百个类,一个类里面包含很多属性

    通过 Get-WmiObject 可以找到设备里面所有可以被找到的 WMI 类

    Get-WmiObject -List

    在 Windows 10 设备,右击开始菜单,打开 PowerShell 输入上面代码,就可以看到输出

    在 Get-WmiObject 的参数可以加上计算机是哪个,支持访问局域网可以访问的计算机的信息

    Get-WmiObject -List -ComputerName 192.168.1.29

    尝试在自己的系统输入一下,可以看到很多代码

    PS> Get-WmiObject -List
    
    Name                                Methods              Properties
    ----                                -------              ----------
    __thisNAMESPACE                     {}                   {SECURITY_DESCRIPTOR}
    __Provider                          {}                   {Name}
    __Win32Provider                     {}                   {ClientLoadableCLSID, CLSID, Concurrency, DefaultMachineNam...
    __ProviderRegistration              {}                   {provider}
    __EventProviderRegistration         {}                   {EventQueryList, provider}
    __ObjectProviderRegistration        {}                   {InteractionType, provider, QuerySupportLevels, SupportsBat...
    __ClassProviderRegistration         {}                   {CacheRefreshInterval, InteractionType, PerUserSchema, prov...
    __InstanceProviderRegistration      {}                   {InteractionType, provider, QuerySupportLevels, SupportsBat...
    __MethodProviderRegistration        {}                   {provider}
    __PropertyProviderRegistration      {}                   {provider, SupportsGet, SupportsPut}
    __EventConsumerProviderRegistration {}                   {ConsumerClassNames, provider}
    __NAMESPACE                         {}                   {Name}
    __EventFilter                       {}                   {CreatorSID, EventAccess, EventNamespace, Name...}
    __EventConsumer                     {}                   {CreatorSID, MachineName, MaximumQueueSize}
    __FilterToConsumerBinding           {}                   {Consumer, CreatorSID, DeliverSynchronously, DeliveryQoS...}
    __AggregateEvent                    {}                   {NumberOfEvents, Representative}
    __TimerNextFiring                   {}                   {NextEvent64BitTime, TimerId}
    __Event                             {}                   {SECURITY_DESCRIPTOR, TIME_CREATED}
    __ExtrinsicEvent                    {}                   {SECURITY_DESCRIPTOR, TIME_CREATED}
    Win32_DeviceChangeEvent             {}                   {EventType, SECURITY_DESCRIPTOR, TIME_CREATED}
    Win32_SystemConfigurationChangeE... {}                   {EventType, SECURITY_DESCRIPTOR, TIME_CREATED}
    
    // 后面还有很多

    显示 WMI 类的信息

    从上面列出的任意一个 WMI 类,可以使用下面代码显示这个类里面的属性

    PS> Get-WmiObject -Class Win32_OperatingSystem
    
    
    SystemDirectory : C:WINDOWSsystem32
    Organization    :
    BuildNumber     : 17763
    RegisteredUser  : lindexi_gd@outlook.com
    SerialNumber    : 00331-10000-00001-AA523
    Version         : 10.0.17763

    这里的输出只是简要的信息,没有包含所有的属性,如果想输出所有的属性,可以使用下面代码

    PS> Get-WmiObject -Class Win32_OperatingSystem | Get-Member -MemberType Property
    
    Name                                      MemberType Definition
    ----                                      ---------- ----------
    BootDevice                                Property   string BootDevice {get;set;}
    BuildNumber                               Property   string BuildNumber {get;set;}
    BuildType                                 Property   string BuildType {get;set;}
    Caption                                   Property   string Caption {get;set;}
    CodeSet                                   Property   string CodeSet {get;set;}
    CountryCode                               Property   string CountryCode {get;set;}
    CreationClassName                         Property   string CreationClassName {get;set;}
    CSCreationClassName                       Property   string CSCreationClassName {get;set;}
    CSDVersion                                Property   string CSDVersion {get;set;}
    CSName                                    Property   string CSName {get;set;}
    // 还有很多属性

    总结一下,获取一个 WMI 类的简洁属性,可以通过这个格式

     Get-WmiObject -Class 某个类

    具体的类可以通过 Get-WmiObject -List 找到

    获取某个类里面包含的所有属性,通过这个格式

    Get-WmiObject -Class 某个类  | Get-Member -MemberType Property

    如果需要获取某个类的某一些属性的值,可以通过下面的代码

    Get-WmiObject -Class 某个类 | Format-Table -Property 属性1,属性2

    如获取 Win32_OperatingSystem 的 TotalVirtualMemorySize 和 RegisteredUser 因为小伙伴的设备和我不相同,可以看到不一样的

    PS> Get-WmiObject -Class Win32_OperatingSystem | Format-Table -Property TotalVirtualMemorySize,RegisteredUser
    
    TotalVirtualMemorySize RegisteredUser
    ---------------------- --------------
                  36052888 lindexi_gd@outlook.com

    如果有很多属性,可以通过列表的方法输出,将 Format-Table 修改为 Format-List 请看下面

    PS> Get-WmiObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ComputerName . | Format-List TotalVirtualMemorySize,TotalVisibleMemorySize,FreePhysicalMemory,FreeVirtualMemory,FreeSpaceInPagingFiles
    
    TotalVirtualMemorySize : 36052888
    TotalVisibleMemorySize : 25042840
    FreePhysicalMemory     : 8510920
    FreeVirtualMemory      : 9954748
    FreeSpaceInPagingFiles : 10482656

    通过 WMI 可以拿很多属性

    Getting WMI Objects Get WmiObject

  • 相关阅读:
    84. Largest Rectangle in Histogram (Solution 2)
    84. Largest Rectangle in Histogram (Solution 1)
    73. Set Matrix Zeroes
    【JavaScript】Symbol 静态方法
    【JavaScript】Date
    【JavaScript】Math
    725. Split Linked List in Parts把链表分成长度不超过1的若干部分
    791. Custom Sort String字符串保持字母一样,位置可以变
    508. Most Frequent Subtree Sum 最频繁的子树和
    762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086360.html
Copyright © 2011-2022 走看看