Enabling DHCP via PowerShell results in 2 WiFi connections to same network

2

2

I have a VM on my network setup as a gateway that I use sometimes, while most of the time I just leave my PCs configured to use DHCP and just have everything go through the router. I had been swapping back and forth manual through the connection properties form but I've decided to try and make PowerShell scripts to simplify the process.

I also disable IPv6 when routing through the VM, but reenable it when I use DHCP. To switch to using the VM as a Gateway I use this:

# Get the adapter to change
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";

# Get current IPv4 address of host
$ip = $wmi.IpAddress[0];

# Configure adapter to use 192.168.1.135 as the default gateway
$wmi.EnableStatic($ip, "255.255.255.0");
$wmi.SetGateways("192.168.1.135", 1);
$wmi.SetDNSServerSearchOrder("192.168.1.135");

# Disable IPv6
Get-NetAdapterBinding | Where-Object {$_.ComponentID.Equals("ms_tcpip6")} | ForEach-Object {
    Disable-NetAdapterBinding -InterfaceAlias $_.Name -ComponentID ms_tcpip6
}

And this works exactly as it does when I configure it manually. My normal network connection changes to "NetworkName 2" in the Network and Sharing Center. To switch back to DHCP, I run this script:

# Get the adapter to change
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";

# Configure adapter to use DHCP and automatic DNS
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();

# Enable IPv6
Get-NetAdapterBinding | Where-Object {$_.ComponentID.Equals("ms_tcpip6")} | ForEach-Object {
    Enable-NetAdapterBinding -InterfaceAlias $_.Name -ComponentID ms_tcpip6
}

And afterwards, I have two connections through WiFi to my network, a Private network called "NetworkName" and a Public network called "NetworkName 3." IPv6 and DHCP are enabled on both.

What is it with my DHCP script that causes this? It didn't happen when I switched back to DHCP through the dialog.

Update: I've noticed this when running ipconfig after switching back to DHCP:

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : 
   IPv6 Address. . . . . . . . . . . : 2001:48f8:1042:732:889c:d2c1:37c9:804e
   Temporary IPv6 Address. . . . . . : 2001:48f8:1042:732:a085:fec9:46b9:3d8
   Link-local IPv6 Address . . . . . : fe80::889c:d2c1:37c9:804e%4
   IPv4 Address. . . . . . . . . . . : 192.168.1.141
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::4af8:b3ff:fe80:6530%4
                                       192.168.1.135
                                       192.168.1.1

Its still using 192.168.1.135 as a Gateway, along with my normal router - 192.168.1.1.

Tyler O

Posted 2016-07-19T01:17:51.397

Reputation: 21

Answers

2

That has been commented on here. Could you not remove the gateway by using this command after you've got the adapter to change stored in $wmi

Remove-NetRoute -InterfaceIndex $wmi.InterfaceIndex -NextHop 192.168.1.135

Alternatively, you could use netsh, as indicated by the same article:

Invoke-Expression ("netsh interface ipv4 set address index=""{0}"" source=dhcp" -f $wmi.InterfaceIndex)

Netsh behaves the same as the GUI.

Stu

Posted 2016-07-19T01:17:51.397

Reputation: 89