Netsh and Local Area Connection Configuration

I have a script that I'm using with my vmware desktops to set the IP address for a linked clone vm upon a refresh, recompose, or the like.  The problem I run into is that the "Local Area Connection" is not the same label on each VM.  Some have a number at the end.  Like "Local Area Connection 2".  Is there anyway to adjust the script to put a wildcard so that the connection gets changed regardless of the number that's at the end of the connection name?  Here is my script...

IF %computername%==IT2015-1 NETSH interface ip set address name= "Local Area Connection 7" static 10.50.150.11 255.255.255.0 10.50.150.1
IF %computername%==IT2015-1 NETSH interface ip add dns name= "Local Area Connection 7" 10.100.0.11 index=1
IF %computername%==IT2015-1 NETSH interface ip add dns name= "Local Area Connection 7" 10.50.110.30 index=2

Open in new window

LVL 1
Anthony6890Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try if this works for you; if it doesn't, please post the output of 'netsh.exe interface show interface':
@echo off
setlocal
set Search=Local Area Connection
set /a InterfaceCount = 0
for /f "tokens=1,2,3*" %%a in ('netsh.exe interface show interface ^| find /i "%Search%"') do (
	set /a InterfaceCount += 1
	if "%%a"=="Enabled" set InterfaceName=%%d
	echo Found interface: '%%d'
)
if %InterfaceCount% equ 0 (
	echo No enabled interface matching '%Search%' found!
	exit /b 1
)
if %InterfaceCount% gtr 1 (
	echo More than 1 interface matching '%Search%' found!
	exit /b 1
)
echo Single interface matching '%Search%' found: '%InterfaceName%'
IF /i %computername%==IT2015-1 (
	netsh.exe interface ip set address name= "%InterfaceName%" static 10.50.150.11 255.255.255.0 10.50.150.1
	netsh.exe interface ip add dns name= "%InterfaceName%" 10.100.0.11 index=1
	netsh.exe interface ip add dns name= "%InterfaceName%" 10.50.110.30 index=2
)

Open in new window

0
Anthony6890Author Commented:
That did work!  

Now I have a quick question regarding your script.  If I have multiple computernames... like IT2015-2 and IT2015-3 and I'm going to set them up, can I then have multiple IF statements below your IF /i %computername% line?
0
oBdACommented:
Well, would you rather define the IP settings inside this script, or in a separate file?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Anthony6890Author Commented:
I could go either way.  I'm going to have to do this for a couple different instance of deployed VM's.  Basically I want to be able to say that...

IT2015-1 IP Address 10.50.150.11
IT2015-2 IP Address 10.50.150.12
and etc

Then define the same Gateway and DNS this group.
0
oBdACommented:
So here's the adjusted script:
@echo off
setlocal
set ConfigFile=C:\Temp\test.txt
set Search=Local Area Connection

set IP=
for /f "tokens=1-4" %%a in ('type "%ConfigFile%" ^| find.exe /i "Netmask="') do (set %%a&set %%b&set %%c&set %%d)
for /f "tokens=2 delims==" %%a in ('type "%ConfigFile%" ^| findstr.exe /i "\<%ComputerName%\>"') do (set IP=%%a)
if "%IP%"=="" (
	echo No IP address for %ComputerName% found in '%ConfigFile%'!
	exit /b 1
)
echo IP configuration for '%ComputerName%':
echo IP:      %IP%/%Netmask%
echo Gateway: %Gateway%
echo DNS:     %DNS1%, %DNS2%

set /a InterfaceCount = 0
for /f "tokens=1,2,3*" %%a in ('netsh.exe interface show interface ^| find /i "%Search%"') do (
	set /a InterfaceCount += 1
	if "%%a"=="Enabled" set InterfaceName=%%d
	echo Found interface: '%%d'
)
if %InterfaceCount% equ 0 (
	echo No enabled interface matching '%Search%' found!
	exit /b 1
)
if %InterfaceCount% gtr 1 (
	echo More than 1 interface matching '%Search%' found!
	exit /b 1
)
echo Single interface matching '%Search%' found: '%InterfaceName%'
netsh.exe interface ip set address name="%InterfaceName%" static %IP% %Netmask% %Gateway%
netsh.exe interface ip add dns name="%InterfaceName%" %DNS1% index=1
netsh.exe interface ip add dns name="%InterfaceName%" %DNS2% index=2

Open in new window

The configuration file has its global settings in the first line, then one line per machine with the IP assignment:
Netmask=255.255.255.0 Gateway=10.50.150.1 DNS1=10.100.0.11 DNS2=10.50.110.30
IT2015-1=10.50.150.11
IT2015-2=10.50.150.12

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony6890Author Commented:
oBdA,

I just tried executing this script with my updated text file and this script doesn't work correctly.  It cannot execute.... should it not be a .bat file?
0
Anthony6890Author Commented:
My bad, typo in my code.  It works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

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.