PowerShell script to achieve the network card DHCP automatically obtain IP address, set static IP address method _powershell

Source: Internet
Author: User
Tags get ip

PowerShell set DHCP to automatically obtain IP addresses, PowerShell can easily set up the local computer to enable DHCP to dynamically obtain IP addresses, which requires a WMI extension. This article describes the detailed scripting procedures.

PowerShell set DHCP to automatically obtain IP addresses

PowerShell makes it easy to set up the local computer to enable DHCP to dynamically obtain IP addresses, which requires a WMI extension. This article describes the detailed scripting procedures.

The first step is to use Get-wmiobject to obtain the specified network adapter configuration object

Copy Code code as follows:
$NETADP = Gwmi Win32_NetworkAdapterConfiguration |? {$_.index-eq 7}

Description
1, Gwmi is Get-wmiobject this cmdlet alias, for the sake of convenience, direct use of GWMI.
2. Win32_NetworkAdapterConfiguration is the object of network card configuration in WMI, it can be used to manipulate the configuration of network card.
3,? {$_.index-eq 7} filters the NIC with an ordinal number of 7 from all the acquired network card objects. This place Hongo to remind everybody to notice, you can not after the pipeline of this condition content, the program will output all the network card information, and then you choose you need to modify the value of the network card information.
4, for each network adapter configuration object, contains the content as follows.

Copy Code code as follows:

Dhcpenabled:true
IPAddress:
DefaultIPGateway:
DNSDomain:
servicename:k57nd60a
Description:broadcom NetLink (TM) Gigabit Ethernet
Index:7

The second step is to view the current DHCP configuration of the selected network card

Copy Code code as follows:

PS c:\users\zhanghong> $NETADP. dhcpenabled
True

The output value of TRUE indicates that DHCP is currently enabled. Of course, if the output will be false to indicate that DHCP is not enabled, a static IP configuration is used.

Step three, open and complete DHCP automatic get IP

Open command:

Copy Code code as follows:

$NETADP. EnableDHCP ()

When you turn off DHCP, you must configure a static IP address:
Copy Code code as follows:

$NETADP. EnableStatic ("192.168.0.2", "255.255.255.0")

OK, about using PowerShell to configure DHCP or static IP addresses, so, I hope to help you.

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.