The basic content of WMI is described in the previous article, which focuses on the view of WMI's property values, and also mentions that WMI has not only attributes but also methods. This section is about how to invoke the WMI object method.
By looking at WMI objects, you can see that a number of classes have both properties and methods. For example, the class "Win32_NetworkAdapterConfiguration", J has a number of methods and properties, you can use the following command query:
Get-wmiobject Win32_NetworkAdapterConfiguration | Get-member-membertype Methods | The 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 property members:
Get-wmiobject Win32_NetworkAdapterConfiguration | Get-member-membertype Property | Format-list through the above command query, class "Win32_NetworkAdapterConfiguration" has the method "EnableDHCP" and the attribute "IPEnabled". The following two members are used to illustrate how to invoke a method of a class.
In this class, the function of the method "EnableDHCP" is to turn on and off the network adapter DHCP function, and the property "Ipenable" is a bool value, whether the response has an IP configuration, or "true" or "false". With "ipenable" You can filter that we can skip any disconnected, virtual network adapter devices.
For example, we can use the following command to change DHCP settings:
Copy Code code as follows:
$Network =get-wmiobject Win32_NetworkAdapterConfiguration | where{
$_. Ipenabled-eq "true"}
foreach ($NIC in $Network) {
$NIC. EnableDHCP ()}
This script first determines whether "ipenable" is true, and if so, turns on DHCP or does not operate.
Class "Win32_NetworkAdapterConfiguration" also has other methods, such as "$NIC. SetDNSServerSearchOrder (), you can use this method to change the DNS settings to change the "Automatically obtain DNS" setting.
Copy Code code as follows:
$Network =get-wmiobject Win32_NetworkAdapterConfiguration | Where{$_. Ipenabled-eq "true"}
foreach ($NIC in $Network) {
$NIC. EnableDHCP ()
$NIC. SetDNSServerSearchOrder ()
}
It is important to note that different types of data parameters are required when calling different methods. In the command output of the previous query method, you can see the specific data format requirements in the Definition field: