WMI EnableDHCP fails on disconnected adapter
It seems at least one other person is interested so here's what I found:
As far as I can tell it's a bug in WMI. To get around it, what seems to work is to directly set the appropriate registry values for the adapter. These are stored in the registry under
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\<guid>
The guid is contained in the IP_ADAPTER_INFO structure filled in by GetAdaptersInfo.
Set the EnableDHCP DWORD value to 1.
I also found it useful to clear the gateways using WMI SetGateways otherwise the old gateway(s) hang around.
I also found that when EnableStatic says it failed, it actually works.
This stuff is only relevant when the network is disconnected.
markh44's answer is still relevant 11 years later.
I found I had to add one additional step though: Set the DisableDhcpOnConnect
value to 0. If I only EnableDHCP
to 1, I found in the IPv4 Properties window that the IP address and subnet mask were gone, but "Use the following address" was still selected. Setting DisableDhcpOnConnect
to 0 caused "Obtain an IP address automatically" to be selected.