Explore PowerShell (14) Methods for using WMI objects _powershell

Source: Internet
Author: User
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:

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.