netsh Interface name store as variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shokarta
Posts: 17
Joined: 14 Oct 2012 05:54

netsh Interface name store as variable

#1 Post by shokarta » 13 Oct 2023 02:17

hello guys,

so netsh interface show interface gives me this:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Wi-Fi
Enabled        Connected      Dedicated        Ethernet 2
Enabled        Disconnected   Dedicated        Ethernet
and I am looking for a way to store the Interface Name which has state connected (in this example it wold be "Ethernet 2" as variable,
so later on I can do soemthing like this:

Code: Select all

@NETSH interface set interface "Ethernet 2" DISABLED
@NETSH interface set interface "Ethernet 2" ENABLED
but instead hardcoded interface name, i would like to use the one from variable set above...

how can I do that?

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: netsh Interface name store as variable

#2 Post by Batcher » 14 Oct 2023 05:11

test-1.bat

Code: Select all

@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected"') do (
    set "AdapterName=%%b"
)
echo,%AdapterName%
pause

shokarta
Posts: 17
Joined: 14 Oct 2012 05:54

Re: netsh Interface name store as variable

#3 Post by shokarta » 02 Apr 2024 01:11

Hello,

can I kindly ask for one more modification?

So from this, basicaly I see in my interfaces:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Wi-Fi
Enabled        Connected      Dedicated        Ethernet 2
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet
and while it loops it sets the Ethernet 2, then it replaces vy VMnet1, and after all it replaces by VMnet8 as it looks for ANY "CONNECTED" state...
however, I am realy looking for any "Ethernet" network only...

so can I kindly ask to modify the code bellow to only look for Ethernet* networks only?

Thank you

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: netsh Interface name store as variable

#4 Post by ShadowThief » 02 Apr 2024 08:10

Pipe a second findstr for "Ethernet"

Code: Select all

@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') do (
    set "AdapterName=%%b"
)
echo %AdapterName%

shokarta
Posts: 17
Joined: 14 Oct 2012 05:54

Re: netsh Interface name store as variable

#5 Post by shokarta » 14 May 2024 23:26

hello,

sorry but does not work... I tried to figure out myself for long time, but I cant :(

so my:

Code: Select all

netsh interface show interface
returns:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Wi-Fi
Disabled       Disconnected   Dedicated        Ethernet 2
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet
and when I run my test.bat:

Code: Select all

:: set currently connected ethernet as variable
FOR /f "tokens=3*" %%a IN ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') DO (SET "AdapterName=%%b")

:: disconnect LAN if connected
IF "%AdapterName:~0,8%"=="Ethernet" (NETSH interface set interface "%AdapterName%" DISABLED)
ECHO LAN disconnected, AdapterName set as %AdapterName%
it only closes the CMD window with error:

Code: Select all

c:\temp\World of Warcraft 5.4.8>test

c:\temp>test.bat
c:\temp>FOR /F "tokens=3*" %a IN ('netsh interface show interface | findstr "Connected" | findstr "Ethernet"') DO (SET "AdapterName=%b" )
DISABLED) was unexpected at this time.
c:\temp>IF "~0,8AdapterName" DISABLED)
can I kindly ask for help here?

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: netsh Interface name store as variable

#6 Post by Squashman » 15 May 2024 08:28

Based on your initial netsh output you do not have any interfaces that are "Connected" and are "Ethernet" What output are expecting when nothing matches?

miskox
Posts: 556
Joined: 28 Jun 2010 03:46

Re: netsh Interface name store as variable

#7 Post by miskox » 15 May 2024 11:38

As Squashman wrote you don't have a match so variable AdapterName remains undefined. Looks like it does not work if variable AdapterName is not defined at all. Looks like IF fails if variable is not defined.
Add

Code: Select all

set AdapterName=doesnotexist
to the beginning of the code.

Code: Select all

set AdapterName=doesnotexist
:: set currently connected ethernet as variable
FOR /f "tokens=3*" %%a IN ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') DO (SET "AdapterName=%%b")

:: disconnect LAN if connected
IF "%AdapterName:~0,8%"=="Ethernet" (
	NETSH interface set interface "%AdapterName%" DISABLED
REM should ERRORLEVEL be checked?
	ECHO LAN disconnected, AdapterName set as %AdapterName%
)
Saso

Post Reply