Explore PowerShell (13) WMI Object Introduction _powershell

Source: Internet
Author: User
Tags server memory
I remember in the XP era, the tools that are often used have a tool called WMI administrative, which is officially provided by Microsoft to view and edit WMI objects, but now seems to not support new systems. However, under Win7, Server 2008, these features can be easily accomplished through PowerShell.

first, let's get to know what a WMI object is:

WMI is present as a basic database in a Windows system. We can connect to the WMI service request to inquire about the information contained therein. WMI includes information on all aspects of the system, including:
• Machine information: manufacturer, model, serial number, etc.
BIOS Information
OS Information
CPU Information: type, manufacturer, speed, version
• Total server memory
• Disk information: capacity, format, etc.
• Network information: MAC, IP, etc.
• Other
You can see how rich the WMI content is, and it covers almost every aspect of the computer.

using PowerShell to view WMI members

In PowerShell, list the WMI objects by using the following command:
Get-wmiobject-list-namespace "root\cimv2″<enter>

You need to view the members of a class specifically, using the following command (for example, class "Win32_Process"):

Get-wmiobject-class win32_process -namespace "root\cimv2" | get-member

You can see that each member is a property, while others are methods (method).

So, there are two questions:

Why use-namespace "root\cimv2"?

Cimv2 is a namespace of WMI, with different WMI object members under each namespace. CIMV2 is its default setting. You can follow these steps to modify:

Control Panel-> Management tools-> Computer Management-> Services and Applications-> right-click WMI Control-> Properties-> Advanced

Parameter "-namespace" is not required, however, using it has two advantages, one is to ensure that we can accurately view the WMI objects under the specified namespace, because sometimes the default namespace is not what we want to view, and the second is that if you do not specify a namespace, the computer that is set up may reject our access request.

Note: Because I use the English version of the system, the individual names in these steps may not be accurate.



In the interface shown above, you can modify the default path.

Another question is, what is the use of viewing the type of a member?

If a member is a method, then we can call it. If a member is a property, we can view its value. However, it is important to note that different attribute members have different data structure, some "System.String", some "system.uint32", some are "system.string[", in use, should pay attention to the data format, otherwise it will be an error.

If we need to manage computers on the network, you need to specify the computer name:

Get-wmiobject-list-namespace "Root\cimv2″-computername Computer name <enter>
OK, now for specific information, see the details.

Cases:

View BIOS Information

Get-wmiobject-class win32_bios-namespace "root\cimv2" <enter>
Run Result:




View service Information

Get-wmiobject-class win32_service-namespace "root\cimv2" | Format-list * <enter> View machine information

Get-wmiobject-class Win32_ComputerSystem | Format-list * <enter>

In most cases in a network management environment, we may want to look at different computer information, so you need to use the parameters of the computer, such as:

Querying network information for the local computer

Copy Code code as follows:

$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
}


If the query object is another machine, simply assign the variable "$name" to other values.

For example:

Copy Code code as follows:

$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
}


With more query commands, you can easily write a query computer information script, at the end of this tutorial, I will provide as many as possible with a variety of common scripts. The contents of this section are first written here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.