Use PowerShell script and netsh to configure IP address

I was working on a PowerShell/PowerCLI script to build a VMware VM from a template, assign IP address, default gateway, DNS, join it into the domain and install some software. I knew Netsh command can be used to configure IP address, default getaway and DNS servers. I did not remember the details how could I find out quickly?

In my experience, the best way to get help is from Netsh context help.

To run a netsh command, you must start netsh from the Cmd.exe prompt and change to the context that contains the command you want to use. In our case, the context is Interface.

G:\>netsh

netsh>interface

To display a list of commands and subcontexts that you can use within a context, at the netsh prompt, type the context name, and then type either /? or help. For example, to display a list of subcontexts and commands that you can use in the Interface context, at the netsh prompt (that is, netsh>), type either of the following:

ip /?

ip help

       

netsh interface>ip ?

The following commands are available:

Commands in this context:
?              - Displays a list of commands.
add            - Adds a configuration entry to a table.
delete         - Deletes a configuration entry from a table.
dump           - Displays a configuration script.
help           - Displays a list of commands.
install        - Install the IP protocol.
reset          - Reset the IP configurations.
set            - Sets configuration information.
show           - Displays information.
uninstall      - Uninstall the IP protocol.

To view help for a command, type the command, followed by a space, and then
 type ?.


To get set command help.

       
netsh interface>ip set

The following commands are available:

Commands in this context:
set address    - Sets the IP address or default gateway to an interface.
set compartment - Modifies compartment configuration parameters.
set dnsservers - Sets DNS server mode and addresses.
set dynamicportrange - Modifies the range of ports used for dynamic port assignm
ent.
set global     - Modifies global configuration general parameters.
set interface  - Modifies interface configuration parameters for IP.
set neighbors  - Sets a neighbor address.
set route      - Modifies route parameters.
set subinterface - Modifies subinterface configuration parameters.
set winsservers - Sets WINS server mode and addresses.
netsh interface>


set address is what I need.

       
netsh interface>ip set address
One or more essential parameters were not entered.
Verify the required parameters, and reenter them.
The syntax supplied for this command is not valid. Check help for the correct sy
ntax.

Usage: set address [name=]
             [[source=]dhcp|static]
             [[address=][/] [[mask=]]
             [[gateway=]|none [gwmetric=]]
             [[type=]unicast|anycast]
             [[subinterface=]]
             [[store=]active|persistent]

Parameters:

       Tag            Value
       name         - Interface name or index.
       source       - One of the following values:
                      dhcp: Enables DHCP for configuring IP addresses for
                            the specified interface.
                      static: Disables DHCP for configuring IP addresses for
                            the specified interface. This value must be
                            specified if an address or a gateway is being
                            configured.
       address      - IPv4 address to add or modify, optionally followed by
                      the subnet prefix length.
       mask         - The IP subnet mask for the specified IP address.
       gateway      - One of the following values:
                      : A specific default gateway for the
                                      static IP address you are setting.
                      none: No default gateways are set.  This is the default.
       gwmetric     - The metric for the default gateway. This field should
                      be set only if gateway is specified.
       type         - One of the following values:
                      unicast: Marks the address as a unicast address.
                               This is the default.
                      anycast: Marks the address as an anycast address.
       subinterface - LUID of the subinterface on which the default gateway
                      exists.  This parameter is only needed on interfaces
                      with multiple subinterfaces.
       store        - One of the following values:
                      active: Set only lasts until next boot.
                      persistent: Set is persistent.  This is the default.

Remarks: Used to enable or disable DHCP for IP address configuration.
         Also removes any previous static IP addresses and default gateways,
         and can add a new static IP address and default gateway.

Examples:

       set address name="Local Area Connection" source=dhcp
       set address "Local Area connection" static 10.0.0.9 255.0.0.0 10.0.0.1 1

netsh interface>

       

Now I found the command to setup IP address. I used the same way found the command I need for DNS configuration. Here are all commands I used.

Configure static IP address 192.168.1.100/24 and gateway 192.168.1.1 to connection called Ethernet0.

netsh interface ip set address “Ethernet0” static 192.168.1.1 255.255.255.0 192.168.1.1

 

Configure primary DNS server 192.168.1.2 to connection called Ethernet0.

netsh interface ip set dnsservers name=”Ethernet0″ static 192.168.1.2 primary no

 

Configure secondary DNS server 192.168.2.2 to connection called Ethernet0.

netsh interface ip add dnsservers “Ethernet0” 192.168.2.2 index=2 no

 

Configure third DNS server 192.168.1.3 to connection called Ethernet0.

netsh interface ip add dnsservers “Ethernet0” 192.168.1.3 index=3 no

 

Example in PowerShell scripts to configure the NIC with static IP address, default gateway and 3 DNS servers show below. $vCenterServerName need to be replace with your vCenter server name, $VMName is the name of the virtual machine. You also need replace your Guest OS administrator account user name and password to make it work.

       

$NetworkSettings = 'netsh interface ip set address "Ethernet" static 192.168.1.1 255.255.255.0 192.168.1.1;

netsh interface ip set dnsservers name="Ethernet" static 192.168.1.2 primary no;

netsh interface ip add dnsservers "Ethernet" 192.168.2.2 index=2 no;

netsh interface ip add dnsservers "Ethernet" 192.168.1.3 index=3 no’

Connect-VIServer -Server $vCenterServerName -WarningAction SilentlyContinue

Invoke-VMScript -ScriptText $NetworkSettings -VM $VMName  -GuestUser administrator -GuestPassword adminpassword
      

When I run the script, I got below error message.

The configured DNS server is incorrect or does not exist.

The script did change the NIC IP settings successfully even with the message. It was probably a timing issue. If you have the same issue, you can either ignore it or add –No at the end of netsh command to ignore the DNS server check. So the new command string is:

       
$NetworkSettings = ' netsh interface ip set address "Ethernet0" static 192.168.1.1 255.255.255.0 192.168.1.1;

netsh interface ip set dnsservers name="Ethernet0" static 192.168.1.2 primary no;

netsh interface ip add dnsservers "Ethernet0" 192.168.1.3 index=2 no;

netsh interface ip add dnsservers "Ethernet0" 192.168.1.3 index=3 no’

Connect-VIServer -Server $vCenterServerName -WarningAction SilentlyContinue

Invoke-VMScript -ScriptText $NetworkSettings -VM $VMName  -GuestUser administrator -GuestPassword adminpassword
       

1 Comment on “Use PowerShell script and netsh to configure IP address

  1. I have multiple nework adapters on my machine. One is ethernet, good for local connection since the (only) internet provider does not offer faster than 380K DSL in this part of town. My cellular USB dongle is much faster, but datacap. And when I connect it windows goes, ethernet is faster, must use that. Or defaults to null static IP and DNS address after each reboot.

    Thanks for the useful information. Going to try to make powershell script that run when I plugin the USB cellular dongle, and have it update network settings for me. Tired of doing this every time I reboot. What is funny is it just works on my Linux laptop.

Leave a Reply

Your email address will not be published. Required fields are marked *