12. PowShell- 对WMI对象的操作

  • PowerShell -- 对WMI对象的操作

     

  1. 什么是WMI 对象

它是作为一个基本的数据库存在于windows 系统中的。我们可以连接WMI服务请求查询其中所包含的信息。Root\cimv2是WMI的一个命名空间,每个命名空间下有不同的WMI对象成员。CIMV2是其默认设置;

WMI包括系统各方面的内容:

机器信息:制造商,型号,序列号等

BIOS信息;

OS 信息;

CPU信息: 种类,制造商,速度,版本

服务器内存总量

磁盘信息:容量、格式等

网络信息:MAC,IP等

其他信息

  1. 使用PowerShell查看 WMI 对象

    实例一:列出WMI对象

    Get-wmiobject –list –namespace “root\CIMV2”

    部分结果:

    PS C:\> Get-wmiobject -list -namespace "root\CIMV2"

    NameSpace: ROOT\CIMV2

    Name                               Methods              Properties

    ----                               -------              ----------

    __SystemClass                      {}                   {}

    __thisNAMESPACE                    {}                  {SECURITY_DESCRIPTOR}

    __Provider                         {}                   {Name}

    __Win32Provider                    {}                   {ClientLoadableCLSI...

    __ProviderRegistration             {}                   {provider}

    __EventProviderRegistration        {}                  {EventQueryList, pr...

    __ObjectProviderRegistration       {}                  {InteractionType, p...

    __ClassProviderRegistration        {}                  {CacheRefreshInterv...

    __InstanceProviderRegistration     {}                  {InteractionType, p...

    __MethodProviderRegistration       {}                   {provider}

    __PropertyProviderRegistration     {}                   {provider,Supports...

    __EventConsumerProviderRegistration {}                   {ConsumerClassNames...

    __NAMESPACE                        {}                   {Name}

    __IndicationRelated                {}                   {}

    __EventFilter                      {}                   {CreatorSID,EventA...

    __EventConsumer                    {}                   {CreatorSID,Machin...

    __FilterToConsumerBinding          {}                   {Consumer,CreatorS...

    __AggregateEvent                    {}                   {NumberOfEvents, Re...

    __TimerNextFiring                  {}                  {NextEvent64BitTime...

    __Event                            {}                  {SECURITY_DESCRIPTO...

    __ExtrinsicEvent                    {}                   {SECURITY_DESCRIPTO...

    Win32_DeviceChangeEvent            {}                   {EventType,SECURIT...

    实例二:查看具体某个类的成员,例如类(win32_process)

    Get-wmiobject –class win32_process –namespace “root\CIMV2” | get-member

    部分结果:

    PS C:\> Get-wmiobject -class win32_process -namespace"root\CIMV2" | get-member

    TypeName:System.Management.ManagementObject#root\CIMV2\Win32_Process

    Name                      MemberType     Definition

    ----                      ----------     ----------

    Handles                   AliasProperty  Handles =Handlecount

    ProcessName                AliasProperty  ProcessName = Name

    PSComputerName            AliasProperty  PSComputerName =__SERVER

    VM                        AliasProperty  VM = VirtualSize

    WS                        AliasProperty  WS = WorkingSetSize

    AttachDebugger             Method         System.Management.ManagementBaseOb...

    GetAvailableVirtualSize   Method        System.Management.ManagementBaseOb...

    GetOwner                  Method        System.Management.ManagementBaseOb...

    GetOwnerSid                Method         System.Management.ManagementBaseOb...

    SetPriority               Method        System.Management.ManagementBaseOb...

    Terminate                 Method        System.Management.ManagementBaseOb...

    Caption                   Property       string Caption{get;set;}

    CommandLine               Property       string CommandLine{get;set;}

    CreationClassName         Property       stringCreationClassName {get;set;}

    实例三:查看BIOS信息

    Get-wmiobject –class win32_bios –namespace “root\CIMV2”

    结果:

    PS C:\> Get-wmiobject -class win32_bios -namespace"root\CIMV2"

    SMBIOSBIOSVersion : A05

    Manufacturer      : Dell Inc.

    Name              : Default SystemBIOS

    SerialNumber      : GB1Q53X

    Version           : DELL   - 6222004

    实例四:查看服务信息

    Get-wmiobject –class win32_service  –namespace “root\CIMV2”  | format-list *

     

    部分结果:

    PS C:\> Get-wmiobject -class win32_service  -namespace "root\CIMV2"  | format-li

    st *

    PSComputerName          : COMPUTER1

    Name                    :AdobeARMservice

    Status                  : OK

    ExitCode                : 0

    DesktopInteract         : False

    ErrorControl            : Ignore

    PathName                :"C:\Program Files (x86)\Common

    Files\Adobe\ARM\1.0\armsvc.exe"

    ServiceType             : OwnProcess

    StartMode               : Auto

    __GENUS                 : 2

    __CLASS                 :Win32_Service

    __SUPERCLASS            :Win32_BaseService

    __DYNASTY               : CIM_ManagedSystemElement

    __RELPATH               :Win32_Service.Name="AdobeARMservice"

    __PROPERTY_COUNT        : 25

    __DERIVATION            :{Win32_BaseService, CIM_Service, CIM_LogicalElement,

    CIM_ManagedSystemElement}

    __SERVER                : COMPUTER1

    __NAMESPACE             :root\CIMV2

    __PATH                  : \\COMPUTER1\root\CIMV2:Win32_Service.Name="Adobe

    ARMservice"

    AcceptPause             : False

    AcceptStop              : True

    Caption                 : AdobeAcrobat Update Service

    CheckPoint              : 0

    CreationClassName       :Win32_Service

    Description             : AdobeAcrobat Updater keeps your Adobe software up

    to date.

    DisplayName             : AdobeAcrobat Update Service

    InstallDate             :

    ProcessId               : 1780

    ServiceSpecificExitCode : 0

    Started                 : True

    StartName               :LocalSystem

    State                   : Running

    SystemCreationClassName : Win32_ComputerSystem

    SystemName              : COMPUTER1

    TagId                   : 0

    WaitHint                : 0

    Scope                   :System.Management.ManagementScope

    Path                    : \\COMPUTER1\root\CIMV2:Win32_Service.Name="Adobe

    ARMservice"

    实例五:查看机器信息

    get-wmiobject -class win32_computersystem |format-list * <enter>

    部分结果:

    PS C:\> get-wmiobject -class win32_computersystem | format-list *

    PSComputerName              : COMPUTER1

    AdminPasswordStatus         : 0

    BootupState                 :Normal boot

    ChassisBootupState          : 3

    KeyboardPasswordStatus      : 0

    PowerOnPasswordStatus       : 0

    PowerSupplyState            : 3

    PowerState                  : 0

    FrontPanelResetStatus       : 0

    ThermalState                : 3

    Status                      : OK

    Name                        : COMPUTER1

    PowerManagementCapabilities :

    PowerManagementSupported    :

    __GENUS                     : 2

    __CLASS                     :Win32_ComputerSystem

    __SUPERCLASS                :CIM_UnitaryComputerSystem

    __DYNASTY                   :CIM_ManagedSystemElement

    __RELPATH                   :Win32_ComputerSystem.Name="COMPUTER1"

    __PROPERTY_COUNT            : 60

    __DERIVATION                :{CIM_UnitaryComputerSystem, CIM_ComputerSystem,

    CIM_System, CIM_LogicalElement...}

    __SERVER                    : COMPUTER1

    __NAMESPACE                 :root\cimv2

    __PATH                      : \\COMPUTER1\root\cimv2:Win32_ComputerSystem.

    Name="COMPUTER1"

    AutomaticManagedPagefile    : True

    AutomaticResetBootOption    : True

    AutomaticResetCapability    : True

    实例六:查询本地计算机的网络信息

    $name="."

    $items = get-wmiObject -class win32_NetworkAdapterConfiguration  -namespace"root\CIMV2" -ComputerName $name | where{$_.IPEnabled -eq“True”}

    foreach($objin $items) {

    Write-Host "DHCP Enabled:"$obj.DHCPEnabled

    Write-Host "IP Address:" $obj.IPAddress

    Write-Host "Subnet Mask:"$obj.IPSubnet

    Write-Host "Gateway:"$obj.DefaultIPGateway

    Write-Host "MAC Address:"$ojb.MACAddress

    }

    实例七:查询其他计算机的网络信息

    $name=read-host "Enter Computer Name"

    write-host "Computer:"$name

    $items = get-wmiObject -class win32_NetworkAdapterConfiguration ‘

    -namespace "root\CIMV2" -ComputerName $name |where{$_.IPEnabled -eq “True”}

    foreach($obj in $items) {

    Write-Host "DHCP Enabled:" $obj.DHCPEnabled

    Write-Host "IP Address:" $obj.IPAddress

    Write-Host "Subnet Mask:" $obj.IPSubnet

    Write-Host "Gateway:" $obj.DefaultIPGateway

    Write-Host "MAC Address:" $ojb.MACAddress

    }

     

  2. PowerShell 使用其他WMI对象的方法
  1. 查询方法

    Get-WmiObject Win32_NetworkAdapterConfiguration | Get-Member -MemberTypeMethods | Format-List

    列出的可用的方法有:

    DisableIPSec

    EnableDHCP

    EnableIPSec

    EnableStatic

    ReleaseDHCPLease

    RenewDHCPLease

    SetDNSDomain

    SetDNSServerSearchOrder

    SetDynamicDNSRegistration

    SetGateways

    SetIPConnectionMetric

    SetIPXFrameTypeNetworkPairs

    SetTcpipNetbios

    SetWINSServer

    ConvertFromDateTime

    ConvertToDateTime

    Delete

    GetType

    Put

调用方法

实例一:我们可以使用如下命令来改变DHCP设置:

$Network=get-wmiobject win32_NetworkAdapterConfiguration | where{

$_.IPEnabled -eq "true"}

foreach($NIC in $Network){

$NIC.EnableDHCP()}

实例一解释:这段脚本首先会判断"IPEnable"是否为真,如果是,则开启DHCP,否则不进行操作。

实例二:类"Win32_NetworkAdapterConfiguration"还具有另外的方法,如"$NIC.SetDNSServerSearchOrder()",可以使用这个方法改变DNS设置,改变是否“自动获取DNS”的设置。

$Network=get-wmiobject win32_NetworkAdapterConfiguration |where{$_.IPEnabled -eq "true"}

foreach($NIC in $Network){

$NIC.EnableDHCP()

$NIC.SetDNSServerSearchOrder()

}

  1. 查询属性

PSC:\> Get-WmiObject Win32_NetworkAdapterConfiguration | Get-Member –MemberType Property | Format-List

TypeName   :System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter

Configuration

Name       : ArpAlwaysSourceRoute

MemberType : Property

Definition : bool ArpAlwaysSourceRoute {get;set;}

TypeName   :System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter

Configuration

Name       : ArpUseEtherSNAP

MemberType : Property

Definition : bool ArpUseEtherSNAP {get;set;}

TypeName   :System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter

Configuration

Name       : Caption

MemberType : Property

Definition : string Caption {get;set;}

TypeName   :System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter

Configuration

Name       : DatabasePath

MemberType : Property

Definition : string DatabasePath {get;set;}

TypeName   :System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter

Configuration

Name       : DeadGWDetectEnabled

MemberType : Property

Definition : bool DeadGWDetectEnabled {get;set;}

参考:http://marui.blog.51cto.com/1034148/296083

时间: 2024-08-06 11:56:54

12. PowShell- 对WMI对象的操作的相关文章

PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件

本系列文章从最初的初识开始,基本上可以完成一些简单的系统管理了,为了更方便的管理系统,同时为了更好的发掘系统的性能,就需要用到系统提供 的一些高级特性,在Windows Server系列的OS中,如果可以利用最新的特性对系统进行管理,将会是一件非常不错的事情,虽然目前WinServer用的比较少 但是在一些地方还是有用到,尤其当某些场合需要非计算机专业的人员在服务器上进行操作的时候,winServer以其比Unix/Linux简单性更加适合应用.今天 这里就对PS中一些高级特性进行介绍. 一.W

C风格字符串和C++ string 对象赋值操作的性能比较

<<C++ Primer>> 第四版 Exercise Section 4.3.1 部分Exercise 4.2.9 习题如下: 在自己本机执行如下程序,记录程序执行时间: 1 #include "stdafx.h" 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <ctime> 6 7 using namespace

对象属性操作-ios

#import <Foundation/Foundation.h> @class Author; @interface Books : NSObject{ @private NSString *color; int book_id; NSString *box; Author *author; NSArray *relationbook; float price; @public NSString *name; } @property (nonatomic) int size; -(void)

关于WMI对象 Get-WmiObject

一什么是WMI对象 Windows Management Instrumentation (WMI) 是 Windows 系统管理的核心技术,因为它可以按统一的方式公开各种类型的信息. 我们在针对Powershell学习的过程中,经常会看到一些针对WMI调取的命令或脚本,但是依然不知道WMI到底可以执行那些操作. 下面就以一些实例说明相关的问题. 二 Get-Wmiobject 我们需要首先知道自己的windows计算机支持那些可用的WMI类. 可以看到的类大概有上千个,那么这些类都能获取到那些

将插座变量(IBOutlet)关联到*.xib文件中对象 + 将对*.xib对象的操作关联到动作方法(IBAction)

将插座变量(IBOutlet)关联到*.xib文件中对象 以BNRDetailViewController.m和BNRDetailViewController.xib为例(<iOS编程>第10章例子) 1.打开BNRDetailViewController.xib,添加一个UITextField对象: 2.在辅助编辑器中打开BNRDetailViewController.m,方法是:按住Option键并点击项目导航面板中的BNRDetailViewController.m: 3.按住Contr

string 对象及其操作

标准库类型string 标准库类型string表示可变长的字符序列,使用string类型必须首先包含string头文件.作为标准库的一部分,string定义在命名空间std中.接下来的示例都假定了已包含了下述代码: #include <string> using std::string; 本节描述最常用的string操作. 定义和初始化string对象 如何初始化类的对象是由类本身决定的.一个类可以定义很多种初始化对象的方式,只不过这些方式之间必须有所区别:或者是初始值的数量有所区别,或者是初

访问SQL Server WMI对象

访问SQL Server WMI对象 1. 使用Powershell访问 get-wmiobject -list -namespace "root\Microsoft\SqlServer" get-wmiobject -list -namespace "root\Microsoft\SqlServer\ServerEvents\MSSQLSERVER" get-wmiobject -namespace "root\Microsoft\SqlServer\C

php中类和对象的操作

$p1 = new Person('michael');//向Person类的构造函数__construct中传名字 echo($p1->name);//获取对象p1的public实例属性name(注意没有$) $p1->speak();//调用对象p1的实例方法speak echo(Person::$sex);//获取类变量(static $sex) echo(Person::sex);//获取类中的常量(不是static const,没有static const.只有类变量没有类常量.)

黑马程序员_JavaSE学习总结第12天_API常用对象2

------- android培训.java培训.期待与您交流! ----------  12.01 Scanner的概述和构造方法原理 Scanner类概述:JDK5以后用于获取用户的键盘输入 构造方法:public Scanner(InputStream source) public static final InputStream in:“标准”输入流. 此流已打开并准备提供输入数据.通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源. 12.02 Scanner类的hasNe