2
\$\begingroup\$

There is a remote Windows PC that has access to "landline" unmetered WiFi and there is a Verizon 4G LTE MiFi (metered WiFi) as backup. This script checks to see if the PC is connected to any WiFi, connected to metered WiFi, or has any internet connection. It will be called a few times a day by Task Scheduler. Everything seems to work OK while testing at home.

@echo off
SETLOCAL enabledelayedexpansion

rem get computer name
set host=%COMPUTERNAME%
set wifi=
if "%host%"=="HOME" (
    set landline=xxx
    set metered=Verizon-4GLTE-xxx-xxxx
)
if "%host%"=="WORK" (
    set landline="xxx spaces in name xxx"
    set metered=Verizon-4GLTE-xxx-xxxx
)
echo.
echo **************************************************
echo %host%
echo landline wifi = !landline!
echo metered wifi = !metered!
echo **************************************************
echo.
echo.
echo ping test and if no response, connect to !metered! ...
echo *************************************************************************************
ping 8.8.8.8 -n 1 || netsh wlan connect name=!metered!
echo *************************************************************************************
timeout /t 10

echo.
echo Attempting to connect to landline wifi %landline% ...
netsh wlan connect name=%landline%
timeout /t 10

echo.
echo.
echo Fail safe...
echo ping test and if no response, connect to !metered! ...
echo *************************************************************************************
ping 8.8.8.8 -n 1 || netsh wlan connect name=!metered!
echo *************************************************************************************
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can check if you are connected to wifi by using WMIC NICConfig where IPEnabled="True" get DefaultIPGateway | findstr "{" and then checking the error level since without /value switch WMIC NICConfig where IPEnabled="True" get DefaultIPGateway wihtout wifi just prints default gateway and nothing else. Pinging is good too. \$\endgroup\$ Commented May 18, 2020 at 21:13
  • \$\begingroup\$ Do you change your computer name depending if you are at work or at home? \$\endgroup\$ Commented May 18, 2020 at 21:15
  • \$\begingroup\$ @NekoMusume Thanks, I'll look at that command. I run the same script on different computers from a shared Google Drive folder, so any updates to the script propagate immediately. \$\endgroup\$
    – bossturbo
    Commented May 20, 2020 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.