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: