How can I assign the name of the current wifi network to a variable in windows batch scripting? - windows

I am trying to add a line to my start up scrip which would automatically start the VPN if I am connected to WiFi networks outside of my work.
I can get the names from netsh wlan commands, but I am not able to just "pick" the SSID/network name from that.
Is there a way to extract particular information from the netsh output or is there a way to output just the wifi name?
Thanks in advance.

You can use the findstr command to find a string in a commands, specifically the SSID.
Script:
#echo off
set "wifiNetwork=example network"
(netsh wlan show networks | findstr /C:": %wifiNetwork%" >nul 2>&1)&&goto :found
echo Wi-Fi network "%wifiNetwork%" not found
goto :end
:found
echo Wi-Fi network "%wifiNetwork%" found!
:end
pause
NOTE: The findstr command is case-sensitive unless you add the /I option

Related

How to check internet connection in .cmd (batch script) and run another command based on whether WiFi is connected or not?

Logic is as provided under
Setup WiFi Connection
if connection was successful:
execute command1
else
execute command2
WiFi Setup is done using the following command
netsh wlan connect ssid=%ssid% name=%name%
What I'm interested in is how to formulate my if condition.
netsh wlan show interfaces | findstr "SSID.*%ssid%"
IF %ERRORLEVEL% EQU 0 GOTO connected

Automatically connect to wifi network when no internet detected

EDIT: Found a fix without using Task Scheduler.
#echo off
:loop
set addresses=192.168.1.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
timeout /t 5
goto :loop
it repeats every 5 seconds
I am halfway there but have become stuck. I already have a batch file that can connect the laptop back to the network when manually run but wanted it to run when no internet is detected. So I went ahead and bound it to Task Scheduler with the Trigger being the "Microsoft-Windows-NetworkProfile/Operational" with an ID of 10001 (which is the Disconnect ID) but have no luck with it.
I believe it only triggers when it manually gets disconnected from the internet, not when the wireless card drops the signal on its' own. Any ideas as to what I can do?
OS: Windows 8.1
Wireless Interface: Cable Matters AC600 Dual Band Wireless dongle (doesn't allow Auto Connect to SSID and keeps dropping the connection for unknown reasons)
Found a solution!
#echo off
:loop
set addresses=192.168.1.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
timeout /t 5
goto :loop
What this does is ping an address once (in this case an internal address so I don't get DDoS threats from sites) and if it responds back with an error, it will load a wifi profile.
To get the profile first you will need to type
netsh wlan show profiles
to see what profiles you have (it only loads profiles for the wireless card currently in use). After seeing the names of the profiles you can download the profile by typing
netsh wlan export profile name=[profile name]
I believe it will save to the desktop and from there you can change the code listed at the top of this comment to match the filepath of the xml file to suit your needs.
Cheers!
What about a batch script that regularly performs pings and you inspect the result?
ping some.host > pinged.txt
find "some string which occurs if the network went down" pinged.txt
If %errorlevel% is 0 than the string was found and the connection went down.

Batch file to enable/disable network adapter

I've got a script that is useful at work to change our LAN adapter Static/DHCP which works fine. However, we do need to occasionally disable our network adapter when using commands over the network (in case of conflict on different networks).
This is the code that I've got for the enable/disable commands.
:2
#echo off
netsh wlan show networks | FIND "Wireless network connection" /I /C >NUL 2>NUL
IF %errorlevel% equ 1 (netsh interface set interface "Wireless network connection" DISABLED)
IF %errorlevel% equ 0 (netsh interface set interface "Wireless network connection" ENABLED)
exit /b
When I run the netsh commands separately do execute correctly, meaning there's an issue with my if statement.
When the network adapter is enabled:
netsh wlan show networks | FIND "Wireless network connection" /I /C
1
When the network adapter is disabled:
netsh wlan show networks | FIND "Wireless network connection" /I /C
0
When running the entire code, each run through (regardless of the state of the wireless network adapter, returns 1).
Any suggestions guys?
echo Errorlevel was %errorlevel%
IF %errorlevel% equ 1 (
echo was enabled
netsh interface set interface "Wireless network connection" DISABLED
) else (
echo was disabled
IF %errorlevel% equ 0 (
netsh interface set interface "Wireless network connection" ENABLED
)
)
As you currently have it, the second errorlevel will interpret the results of the first netsh if that is effected.
don't confuse command output and %errorlevel%:
C:> echo yes|find "yes" /c
1
C:> echo %errorlevel%
0
C:> echo yes|find "no" /c
0
C:> echo %errorlevel%
1
%errorlevel% is set, when the last command (find) was unsuccessful, while find /c returns the count of findings. No findings means find /c returns 0 and an %errorlevel% of 1.

How to Loop Wifi connect command while disconnected, until connected, in a batch file?

I read up on For loops but im not sure how to integrate an IF statement to make it look more like a WHILE statement.
Windows Task scheduler will pick up disconnect events and run my batch file.
While wifi disconnected from "GUHOA" SSID run the "netsh wlan connect name=GUHOA" command every 5 seconds until its connected then stop.
Any ideas?
This is what im building off of:
netsh interface show interface | find "Enabled Connected Dedicated Wireless Network Connection" /I /C
IF %ERRORLEVEL% equ 1 (
netsh wlan connect name=GUHOA | echo Connecting to GUHOA
) ELSE (
echo Already connected
)
a for might not be the best solution here. Use a simple loop instead:
:loop
netsh interface show interface | find "Enabled Connected Dedicated Wireless Network Connection" /I /C
IF %ERRORLEVEL% equ 0 goto :connected
netsh wlan connect name=GUHOA | echo Connecting to GUHOA
timeout 5 >nul
goto :loop
:connected
echo I'm connected.
note: with netsh ... | echo something you pipe the output of netsh to echo, so you won't see it (but maybe that's your intention)

batch file to detect if wifi adapter is enabled

I tried to complete this commands in cmd but im having trouble fixing it. Can anyone help me?
netsh wlan show networks | FIND "turned off" /I /C
if "dont know what should be here" == 0 (
echo enabled
) else (
echo disabled
)
pause
You're looking for the %errorlevel% variable, which indicates the exit status of the command last executed (in your case find). You have to revert your logic, though, because find returns 0 (i.e. "success") when the adapter is disabled. Also, I'd recommend to do a numeric comparison (equ) instead of a string comparison (==).
if %errorlevel% equ 0 (
echo disabled
) else (
echo enabled
)
All you have to do is attempt to enable it whether or not it's enabled or disabled
netsh interface set interface name="name of adapter" admin=enable || echo already enabled
If the adapter is already enabled then it won't do anything so the double pipes || means if there's an error in the first command it will execute whatever command is after it which is echo "already enabled."
If it says "this network connection does not exist" ignore that, it means the adapter is already enabled.
I put together this code for a batch script. It works like a charm to turn ON/OFF my wireless network connection:
netsh wlan show networks | FIND "Wireless network connection" /I /C
if %errorlevel% equ 1 (wmic path win32_networkadapter where NetConnectionID="Conexión de red inalámbrica" call enable) else (wmic path win32_networkadapter where NetConnectionID="Wireless network connection" call disable)
If you have more than one wireless network connection, change the name for your particular network connection name and that will do.

Resources