检查网络标签的 OS 版本和变量

Check OS version and variable for Network Label

然而,我一直在成功地使用这段代码,希望对其进行调整并在执行 netsh 命令时使用它来重命名 NIC - 虽然在写这篇文章时我不确定它是否是最佳途径。

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% )

仅供参考:2012 年之前的网络标签是本地连接,2012 年是以太网。

我要执行以下命令

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

我认为基于操作系统版本 %NETWORKNAME% 将被定义为 "Local Area Connection" 或 "Ethernet"

我可以使用类似

的东西吗
  IF "%_OSVer%" EQU "2003" + "2008" + "2008R2" SET Networkname="Local Area Network"

或者有更好的方法吗?

编辑 - 来自 Pauls Feeback,最终代码是

 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)

谢谢 B

There 是此类问题的宝贵答案。

如果非要我改编的话,会是这样的:

@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

您可能还喜欢:

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

您可以使用 GEQ 和 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