Explore how PowerShell (14th) uses WMI objects

Source: Internet
Author: User

The previous article introduced the basic content of WMI, mainly about viewing the WMI attribute values. It also mentioned that WMI not only has attributes but also methods. This section describes how to call the WMI Object method.

By viewing WMI objects, you can find that many classes have both attributes and methods. For example, for the "Win32_NetworkAdapterConfiguration" type, j has many methods and attributes. You can use the following command to query:

Get-WmiObject Win32_NetworkAdapterConfiguration | Get-Member-MemberType Methods | available Methods listed in Format-List are:

DisableIPSec
EnableDHCP
EnableIPSec
EnableStatic
ReleaseDHCPLease
RenewDHCPLease
SetDNSDomain
SetDNSServerSearchOrder
SetDynamicDNSRegistration
SetGateways
SetIPConnectionMetric
SetIPXFrameTypeNetworkPairs
SetTcpipNetbios
SetWINSServer
ConvertFromDateTime
ConvertToDateTime
Delete
GetType
Put

Similarly, use the following command to query its attribute members:

Get-WmiObject Win32_NetworkAdapterConfiguration | Get-Member-MemberType Property | Format-List is queried using the preceding command. The class "Win32_NetworkAdapterConfiguration" has methods "EnableDHCP" and "IPEnabled ". Next, we will use these two members to illustrate how to call the class method.

In this class, the method "EnableDHCP" is used to enable or disable the DHCP function of the network adapter. The attribute "IPEnable" is a bool value to check whether IP addresses are configured, if yes, it is "true", otherwise it is "false ". "IPEnable" can be used to filter out any disconnected and virtual network Adaptation devices.

For example, we can use the following command to change the DHCP settings:Copy codeThe Code is as follows: $ Network = get-wmiobject win32_NetworkAdapterConfiguration | where {
$ _. IPEnabled-eq "true "}
Foreach ($ NIC in $ Network ){
$ NIC. EnableDHCP ()}

This script first checks whether "IPEnable" is true. If yes, DHCP is enabled. Otherwise, no operation is performed.

The "Win32_NetworkAdapterConfiguration" class also has other methods, such as "$ NIC. SetDNSServerSearchOrder ()". You can use this method to change the DNS settings and whether to set "automatically retrieve DNS.Copy codeThe Code is as follows: $ Network = get-wmiobject win32_NetworkAdapterConfiguration | where {$ _. IPEnabled-eq "true "}
Foreach ($ NIC in $ Network ){
$ NIC. EnableDHCP ()
$ NIC. SetDNSServerSearchOrder ()
}

Note that different types of data parameters are required when calling different methods. In the command output of the preceding query method, you can see the specific data format requirements in the "Definition" field:

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.