Vanelder
Vanelder

Reputation: 85

Identify NIC Name from netsh and do something

I'm expanding on one of my previous posts.

Objective for this is to double check that the network labels exist, if yes the script will continue to apply IP details, if no then we need to check if devcon failed to run or virtual network cards were not correctly added.

FYI: This is for virtual machine migration, once migrated there are no network cards, we run devcon to clear out all previous adapters/hardware and add in new network cards which should be in the correct order (Local Area Connection, Local Area Connection 2 and Local Area Connection 3) but because windows this isn't always the case :( so some sanity checks are required.

What I have so far is as follows.

SET NICNaming=OTHER
FOR /F "Tokens=2 Delims=[]" %%a IN ('VER') DO SET _VerNo=%%a
FOR /F "Tokens=2-3 Delims=. " %%b IN ("%_VerNo%") DO (
    IF "%%b.%%c" LEQ  "6.1" SET NICNaming=Local Area Connection
    IF "%%b.%%c" GEQ  "6.2" SET NICNaming=Ethernet

)

for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do (

IF "%%B" EQU "%NICNaming%" (ECHO %NICNaming% FOUND!) ELSE (ECHO %NICNaming% MISSING!)
IF "%%B" EQU "%NICNaming% 2" (ECHO %NICNaming% 2 FOUND!) ELSE (ECHO %NICNaming% 2 MISSING!)
IF "%%B" EQU "%NICNaming% 3" (ECHO %NICNaming% 3 FOUND!) ELSE (ECHO %NICNaming% 3 MISSING!)
)

It outputs sort of correctly but is doubling up the information, like so - any ideas why?

Local Area Connection MISSING!
Local Area Connection 2 FOUND!
Local Area Connection 3 MISSING!
Local Area Connection MISSING!
Local Area Connection 2 MISSING!
Local Area Connection 3 MISSING!

So ideally, once it's functioning correctly and if any of the 3 new NIC's added are missing or not named correctly the will warn the user for them to remediate the problems.

Thanks B

EDIT: Wox's response fixed issue - correct code below!

SET NICNaming=OTHER
FOR /F "Tokens=2 Delims=[]" %%a IN ('VER') DO SET _VerNo=%%a
FOR /F "Tokens=2-3 Delims=. " %%b IN ("%_VerNo%") DO (
    IF "%%b.%%c" LEQ  "6.1" SET NICNaming=Local Area Connection
    IF "%%b.%%c" GEQ  "6.2" SET NICNaming=Ethernet

)

for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do (

IF "%%B" EQU "%NICNaming%" (ECHO %NICNaming% FOUND!) ELSE (ECHO %NICNaming% MISSING!)
IF "%%B" EQU "%NICNaming% 2" (ECHO %NICNaming% 2 FOUND!) ELSE (ECHO %NICNaming% 2 MISSING!)
IF "%%B" EQU "%NICNaming% 3" (ECHO %NICNaming% 3 FOUND!) ELSE (ECHO %NICNaming% 3 MISSING!)
Goto Done
)
:Done

Upvotes: 2

Views: 511

Answers (1)

woxxom
woxxom

Reputation: 73686

Apparently netsh interface show interface shows two lines, so the last loop executes twice.

Break out of the loop after one iteration:

for ......... (
    ..........
    goto done
)
:done

Upvotes: 1

Related Questions