• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2297
  • Last Modified:

WIN 7 .bat file to change IP address .. DHCP problem

I have a few batch files that I use to switch NIC settings.  These are all "Run as Administrator".

For static IP, I use this:
@ECHO OFF
set varip=192.168.0.99
set varsm=255.255.255.0
set vargw=192.168.0.1
set vargwmetric=1
set vardns1=192.168.0.1
set vardns2=8.8.8.8
set vardnsindex1=1
set vardnsindex2=2

REM ***** You don’t need to change anything below this line! ******
ECHO Setting IP, mask, gateway and DNS
REM netsh set address "Local Area Connection" static %varip% %varsm% %vargw% %vargwmetric%
netsh interface ip set address "Local Area Connection" static %varip% %varsm% %vargw% %vargwmetric%
ECHO Setting Primary DNS
netsh interface ip set dns name="Local Area Connection" source=static address=none
ECHO Setting primary DNS
netsh interface ip add dns  name="Local Area Connection" %vardns1% index=%vardnsindex1%
ECHO Setting secondary DNS
netsh interface ip add dnsserver "Local Area Connection" %vardns2% index=%vardnsindex2%
ECHO Here are the new settings for %computername%:
netsh int ip show config
pause
end

Open in new window


To switch to DHCP "get an address automatically", I use this:
@ECHO OFF
REM ***** You don’t need to change anything below this line! ******
netsh interface ip set address name="Local Area Connection" dhcp
netsh interface ip set dns name="Local Area Connection" dhcp
netsh interface ip set default gateway name="Local Area Connection" dhcp
netsh interface ip show config[embed=file 906425]
pause
end

Open in new window

The problem is, that when I switch from a static address to a DHCP configuration, the default gateway IP address remains and the DHCP button isn't turned on - and the NIC settings dialog appears to disagree with the results of ipconfig that says DHCP YES!

ipconfig
At the same time, the NIC ipv4 settings look like this:
NIC settings
What might be the problem here?
0
Fred Marshall
Asked:
Fred Marshall
  • 3
  • 2
2 Solutions
 
Zephyr ICTCloud ArchitectCommented:
netsh interface ip set address name="Local Area Connection" dhcp

You have name="Local Area Connection" but not source=dhcp

Did you try it like that?
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Instead of ip - you should use ipv4, the command would be:

netsh interface ipv4 set address name="Local Area Connection" source=dhcp

Open in new window

The above command will switch DNS to automatically as well.

https://technet.microsoft.com/en-us/library/cc731521%28v=ws.10%29.aspx#BKMK_setaddress
0
 
Fred MarshallPrincipalAuthor Commented:
I tried those things but nothing in the results has changed.

ipconfig result and the GUI result are different re: DHCP.
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
Gerwin Jansen, EE MVETopic Advisor Commented:
This is Windows 7 right? I tested on W7 64bit machine and it works just fine? Any error message you see in Windows event log maybe?
0
 
Fred MarshallPrincipalAuthor Commented:
reboot...  :-(
Everything works now.
Thanks all!
0
 
Fred MarshallPrincipalAuthor Commented:
Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now