netsh Interface name store as variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shokarta
Posts: 19
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: 19
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: 1164
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: 19
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: 564
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

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

Re: netsh Interface name store as variable

#8 Post by shokarta » 15 May 2024 23:08

Hello,

thank you for the input, actually it worked, however I assume as a workaround,
because basicaly there are only two possible scenarios,

this is my netsh interface show interface before running the script:

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
so basicaly i always want to skip the VMware,
and form here, always either one of Ethernets will be connected (not both), or none of them but Wi-Fi will.
However, its possible that both Wi-Fi and one of the Ethernet will be connected... in this case I wish to prefer to store as variable the connected Ethernet as priority.

I know I have already asked a lot... but can I kindly ask to modify one last time according to this explanation?

Summary:
- skip VMWare(s)
- if both Wi-Fi and one of Ethernet(s) are connected, prefer the Ethernet
- in theory there could be only one of each (wifi or ethernet), or more of each too
- if none is connected (which is weird), i would keep what you just did and at the beginning of script I would manualy do: "SET "AdapterName=Wi-Fi""

Thank you guys, you are real life savers

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

Re: netsh Interface name store as variable

#9 Post by miskox » 16 May 2024 09:58

Try this:

Code: Select all

@echo off
set "AdapterName=Wi-Fi"
:: set currently connected ethernet as variable
FOR /f "tokens=3*" %%a IN ('netsh interface show interface ^| findstr /v "VMware" ^| sort /r /+48 ^| 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%
)

Code: Select all

findstr /v 
removes VMware strings from the output

Code: Select all

sort /r /+48
sorts the output in reverse order (so the Wifi interface is at the top) and sort is based on column 48. Why? Because for /f will set variable AdapterName for each line that contains word 'Connected'. So in this case word 'Ethernet' will be at the bottom of the sort operation and variable will have that value.


Note: searches (findstr) are case sensitive.

Saso

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

Re: netsh Interface name store as variable

#10 Post by shokarta » 23 May 2024 00:04

you guys are the best :)

working perfectly :)

but maybe one more question to this... basicaly little different topic in same script...

well basicaly after I determine this AdapterName, I am connecting my WiFi to different profile:

Code: Select all

@ECHO Waiting for connection to WiFi (MYNETWORK)
@NETSH wlan connect ssid="MYNETWORK" name="MYNETWORK"
:loop_zfguest
@TIMEOUT /t 1 /nobreak >nul
@NETSH interface show interface name="Wi-Fi" | FIND "Connect state" | FIND "Connected" >nul || GOTO :loop_zfguest
which only outputs to me:

Code: Select all

Waiting for connection to WiFi (MYNETWORK)
The network specified by profile "MYNETWORK" is not available to connect.
I know also this was working to me half year ago, but not anymore?
So I was thinking that it does not see other available networks them at the moment, because when I click to Wifi icon in Windows system bar, then it takes couple seconds to load those which are available.
So I was thinking again to refresh this list first by:

Code: Select all

@ECHO Refreshing list of WiFi profiles
@NETSH wlan show profiles
which gives me big list of all wifis which I ever been connected to:

Code: Select all

Refreshing list of WiFi profiles

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
    ZFOFFICE

User profiles
-------------
    All User Profile     : SomeWifiName1
    All User Profile     : SomeWifiName2
    All User Profile     : SomeWifiName3
    All User Profile     : SomeWifiName4
    All User Profile     : SomeWifiName5
    All User Profile     : SomeWifiName6
    All User Profile     : SomeWifiName7
    All User Profile     : SomeWifiName8
    All User Profile     : SomeWifiName9
    All User Profile     : SomeWifiName10
    All User Profile     : MYNETWORK

Waiting for connection to WiFi (MYNETWORK)
The network specified by profile "MYNETWORK" is not available to connect.
So you can cleary see the last one realy exists, thats the one I want to connect.
I can see it available also when I click to the Wifi icon in windows system tray... so why the batch script says its not available?
Can I somehow refresh the list of available networks and not the old stored profiles?

PS: I have tries:

Code: Select all

netsh wlan show networks
but it gives me only one wifi network which I am currently connected to...
but when I click onm the wifi icon at the systray, so I let windows load all networks, and then run the:

Code: Select all

netsh wlan show networks
again, it properly gives me list of all visible availble SSID networks...
so how do I force to refresh the lists to see them all before doing initial:

Code: Select all

@NETSH wlan connect ssid="MYNETWORK" name="MYNETWORK"
?

PS: I found this: https://superuser.com/questions/889414/ ... mmand-line
some command "explorer.exe ms-availablenetowrks:network-wifi" opens the popup menu like when i click to wifi icon in systray manualy, and force windows to load all available networks... can I do it somehow silently so it wont physicaly open?

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

Re: netsh Interface name store as variable

#11 Post by miskox » 23 May 2024 01:45

You can start with this: split this command into separate commands:

Code: Select all

@NETSH interface show interface name="Wi-Fi" | FIND "Connect state" | FIND "Connected" >nul || GOTO :loop_zfguest

Code: Select all

@NETSH interface show interface name="Wi-Fi"
What does the command above return?

And then followed by ((with correct syntax of course - use of a temporary file in neccesary):

Code: Select all

FIND "Connect state"
FIND "Connected" 

Saso

Post Reply