Batch file to enable/disable network adapter - windows

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.

Related

Batch File not running, command works from CMD. IP Change

I am trying to get a basic IP change batch file to work. The commands I am using work in command prompt.
Batch File:
#echo off
netsh interface ipv4 set address name=”Ethernet” static 192.168.1.236 255.255.255.0 192.168.1.1
#echo off
netsh interface ipv4 set dns name=”Ethernet” static 8.8.8.8
pause
I am attempting to use this and another batch that sends it back to DHCP. This file worked 1 time, but doesn't work anymore. I am opening the file as administrator.
I can run these commands in CMD and they work fine. Any suggestions?
- Windows 10
I got this script off of Stack someplace once apon a batch code search. I have modified the DNS to use Google's DNS for the Primary and Secondary DNS's and also made some cosmetic changes for use in one of my own scripts but i'm sure it will answer your question. its has worked alright for me.
All the IP Address are default network setup address with exception to the DNS, you will have to change them manually in a TXT file for your own uses. Just save it as a batch script, if you have a problem, I can find and post the original code, I have it backed up on a cloud somewhere, just send me a message.
Hope it helps.
:again
cls
TITLE Ip Changer
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ECHO ~ 1. Change stack to static IP address, mask, and DNS Server(s) ~
ECHO ~ 2. Change stack to DHCP network configuration ~
ECHO ~ 3. Change stack to SB200 IP configuration ~
ECHO ~ 4. Change stack to SB600B IP configuration ~
ECHO ~ 5. Change stack to T_U configuration ~
ECHO ~ 6. Change stack to T_P configuration ~
ECHO ~ 7. Main Menu ~
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SET /p choice=
IF /I [%choice%]==[1] GOTO Static
IF /I [%choice%]==[2] GOTO DHCP
IF /I [%choice%]==[3] GOTO SB200
IF /I [%choice%]==[4] GOTO SB600
IF /I [%choice%]==[5] GOTO T_U
IF /I [%choice%]==[6] GOTO T_P
IF /I [%choice%]==[7] GOTO menu
CLS
ECHO INCORRECT CHOICE CHOOSE AGAIN
GOTO again
PAUSE>NUL
ECHO.
:Static
ECHO.
SET /P Addy=IP Address:
SET /P Sub_Mask=Subnet Mask:
SET /P GW=Gateway IP Address:
SET /P DNS_1=Primary DNS Server:
SET /P DNS_2=Secondary DNS Server:
netsh interface ip SET address "Local Area Connection" static %Addy% %Sub_Mask% %GW% 1
netsh interface ip delete dns "Local Area Connection" all
IF NOT [%DNS_1%]==[] netsh interface ip SET dns "Local Area Connection" static %DNS_1%
IF NOT [%DNS_2%]==[] netsh interface ip add dns "Local Area Connection" %DNS_2% index=2
ECHO.
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:DHCP
ECHO.
#ECHO Setting up Local Area Connection for DHCP Configuration
netsh interface ip SET address "Local Area Connection" source=dhcp
netsh interface ip SET dns "Local Area Connection" source=dhcp
ECHO.
ipconfig
ECHO.
PAUSE
GOTO again
:SB200
ECHO.
#ECHO 10.10.241.205
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 10.10.241.205 255.255.255.0 10.10.241.201 1
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:SB600
ECHO.
#ECHO 192.168.0.102
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 192.168.0.102 255.255.255.0 192.168.0.1 1
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:T_U
ECHO.
#ECHO 172.16.233.215
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 172.16.233.215 255.255.255.0 172.16.233.214 1
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:T_P
ECHO.
#ECHO 10.10.10.10
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 10.10.10.10 255.255.255.0 10.10.10.1 1
netsh interface ip show config
ECHO.
PAUSE
GOTO :again
P.S. I'v been looking for the person who originally wrote this code. Incase said person reads this, Thanks.

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

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

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 commands to change IP and DNS

I have written few commands in batch to execute them on choice basis. I am using 2 IP addresses for which I have to change IPv4 and DNS every time I switch between IPs.
I have done this code and this works correctly if I execute line by line but in batch they give errors.
#ECHO OFF
SET /P no= Welcome dude so what are you up to press 1 for buzznet,2 for BSNL :
IF "%NO%"=="1" GOTO BUZZ
IF "%NO%"=="2" GOTO BSNL
:BUZZ
netsh interface ipv4 set address name="Ethernet" source=static ^
addr=192.168.22.19 mask=255.255.255.0 gateway=192.168.22.1
netsh interface ip add dns name="Ethernet" addr=192.168.18.1
netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=2
:BSNL
netsh interface ip set address "Ethernet" dhcp
netsh interface ip set dns “Ethernet” dhcp
pause
As stated in comments you need to add something that stop the script continuing when the job is done. (goto:EOF or exit /b 0)
#ECHO OFF
:retry
SET /P no= Welcome dude so what are you up to press 1 for buzznet,2 for BSNL :
IF "%no%"=="1" GOTO BUZZ
IF "%no%"=="2" GOTO BSNL
rem if %no% is not 1 nor 2 then exit or goto :retry.
exit /b 0
:BUZZ
netsh interface ipv4 set address name="Ethernet" source=static ^
addr=192.168.22.19 mask=255.255.255.0 gateway=192.168.22.1
netsh interface ip add dns name="Ethernet" addr=192.168.18.1
netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=2
rem job done, then exit with a pause before
pause
exit /b 0
:BSNL
netsh interface ip set address "Ethernet" dhcp
netsh interface ip set dns "Ethernet" dhcp
pause
Also the last command are malformed with quotes “Ethernet” should be "Ethernet"
I checked this before doing a script to change between a static IP and dynamic so I thought of adding my solution in case it´s useful.
There are two options also to check the status of the network interfaces and to check the current IP.
Everything for the interface called "Ethernet".
Because this needs to run as admin, what I did was this: this bat was stored in my folder, C:/workset/scripts/ then I created a shortcut, copied this on the Desktop.
Then the shortcut, I went to Properties and there I checked the option to Run as admin and also just becase, changed the icon to a cool one.
So now I can run it from the desktop as any other program and it runs as admin from the start.
#ECHO OFF
cls
ECHO ________________________________
ECHO Only works if run as admin.
ECHO.
:MENU
ECHO.
ECHO.
ECHO _______________________________
ECHO MENU
ECHO ................................
ECHO Choose an option:
ECHO.
ECHO 1 IP static 192.X.X.X, 255.X,X.X
ECHO 2 IP dynamic
ECHO 3 Check current IP
ECHO 4 Check network interfaces status
ECHO 5 EXIT
ECHO ..................................
ECHO.
ECHO.
SET /P M=Write 1, 2, 3, 4 or 5 and press ENTER:
IF %M%==1 GOTO STATIC
IF %M%==2 GOTO DYNAMIC
IF %M%==3 GOTO CHECK
IF %M%==4 GOTO CHECKIF
IF %M%==5 GOTO EOF
:STATIC
ECHO ______________________________
ECHO.
ECHO Changing IP to 192.X.X.X and subred mask to 255.X.X.X
netsh interface ip set address name= "Ethernet" static 192.X.X.X 255.X.X.X
TIMEOUT /T 2 /NOBREAK > NUL
ECHO ____________________________
netsh interface ip show config name="Ethernet"
TIMEOUT /T 2 /NOBREAK > NUL
GOTO MENU
:DYNAMIC
ECHO _______________________________
ECHO.
ECHO Changing to a dynamic IP...
netsh interface ip set address "Ethernet" dhcp
TIMEOUT /T 4 /NOBREAK > NUL
ECHO _____________________________
netsh interface ip show config name="Ethernet"
TIMEOUT /T 2 /NOBREAK > NUL
GOTO MENU
:CHECK
ECHO __________________________________
ECHO Showing the adapter details...
netsh interface ip show config name="Ethernet"
TIMEOUT /T 2 /NOBREAK > NUL
GOTO MENU
:CHECKIF
ECHO ____________________________________
ECHO Showing the network interface status....
netsh interface ipv4 show interfaces
TIMEOUT /T 2 /NOBREAK > NUL
GOTO MENU
There should be an error handling but I dont have time, so this is it.
Another note: tbh, at the end I just removed all the options and created one to change to the static IP and another to the dynamic IP so now I just run them faster.

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