首页 > 系统服务 > 详细

12. PowShell- 对WMI对象的操作

时间:2015-04-28 00:02:09      阅读:368      评论:0      收藏:0      [点我收藏+]
  • 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 


本文出自 “Ricky's Blog” 博客,转载请与作者联系!

12. PowShell- 对WMI对象的操作

原文:http://57388.blog.51cto.com/47388/1639367

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!