Search code examples
variablesbatch-filesetenvironmentnetsh

Check OS version and variable for Network Label


I've been using this code successfully however, looking to adjust it and use when performing netsh commands to rename NIC's - although whilst writing this i'm not sure it's the best route anymore.

SET _OSVer=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" EQU "5.2" SET _OSVer=2003
    IF "%%b.%%c" EQU "6.0" SET _OSVer=2008
    IF "%%b.%%c" EQU "6.1" SET _OSVer=2008R2
    IF "%%b.%%c" EQU "6.2" SET _OSVer=2012
    IF "%%b.%%c" EQU "6.3" SET _OSVer=2012R2
)

IF "%_OSVer%" EQU "2003" (ECHO %_OSVer% )
IF "%_OSVer%" EQU "2008" (ECHO %_OSVer% )
IF "%_OSVer%" EQU "2008R2" (ECHO %_OSVer% )
IF "%_OSVer%" EQU "20012" (ECHO %_OSVer% )
IF "%_OSVer%" EQU "20012R2" (ECHO %_OSVer% )

FYI: Pre2012 network labels are Local Area Connection, 2012 is Ethernet.

I want to execute the following command

netsh interface set interface name = "%NETWORKNAME%" newname = "network-storage"  >nul 2>&1

I'm thinking based on the OSversion %NETWORKNAME% would be defined as either "Local Area Connection" or "Ethernet"

Can I use something like

  IF "%_OSVer%" EQU "2003" + "2008" + "2008R2" SET Networkname="Local Area Network"

Or is there a better way to do this?

EDIT - From Pauls Feeback, final code is

 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

)

ECHO %NICNaming%

netsh interface set interface name = "%NICNaming%" newname = "network-storage"  >nul 2>&1
if %errorlevel%== 0 (Echo found) ELSE (Echo missing abort)

Thanks B


Solution

  • There are a valuable answer for such thing.

    If I had to adapt, it would be like this:

    @echo off
    setlocal
    GOTO CHKVERS
    
    :action
    rem action
    SET "Networkname=Local Area Network"
    GOTO:EOF
    
    
    :CHKVERS
    for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
    if "%version%" == "6.3" goto action &REM Windows 8.1
    if "%version%" == "6.2" goto action &REM Windows 8.
    if "%version%" == "6.1" goto action &REM Windows 7.
    if "%version%" == "6.0" goto action &REM Windows Vista.
    
    echo Version undetermined, please contact an administrator.
    
    endlocal
    exit /b 0
    

    You may also like:

    IF "%A%"=="1" IF "%B%"=="1" IF "%C%"=="1" GOTO YES
    GOTO NO
    

    You may use GEQ and LEQ

    IF "%version%" GEQ "6.0" IF "%version%" LEQ "6.3" GOTO YES
    GOTO NO
    

    EQU : Equal

    NEQ : Not equal

    LSS : Less than <

    LEQ : Less than or Equal <=

    GTR : Greater than >

    GEQ : Greater than or equal >=

    This 3 digit syntax is necessary because the > and < symbols are recognised as redirection operators

    http://ss64.com/nt/if.html