用于 WLAN 的 Windows 10 批处理或 powershell 脚本

用于 WLAN 的 Windows 10 批处理或 powershell 脚本

有人能帮我创建一个批处理脚本来自动连接到SSID: SDWLAN如果已经连接,则跳过重新连接以避免断开现有连接。

我的目的是为一组用户设置一个任务,从上午 8 点到下午 5 点每 3 分钟运行一次。

目前这是我所拥有的但没有工作:(并且任何解释或修改都将非常感激。:)

netsh wlan show interface | find "SDWLAN"
IF EXIST "SDWLAN" GOTO end
netsh wlan connect name="SDWLAN"
:end
exit

答案1

您可以像这样编写批处理脚本:

评论 :代码已编辑(在批处理脚本中添加注释)


@echo off
Title Connect to WLAN
@REM -----------------------------------------------------------------------------------
@REM Setting the SSID Name variable here
Set "SSID=SDWLAN"
@REM -----------------------------------------------------------------------------------
@REM Testing the wlan is connected or no by piping it to findstr as a regular expression
@REM The following switches used here with findstr
@REM /I Case-insensitive search
@REM /R Evaluate as a regular expression.
@REM commandA && commandB || commandC
@REM If commandA succeeds run commandB, if commandA fails run commandC
@REM Note that if commandB fails, that will also trigger running commandC.
@REM -----------------------------------------------------------------------------------
netsh wlan show interface | findstr /I /R "%SSID%">nul && (
@REM -----------------------------------------------------------------------------------
@REM if this is true we show that we are connecting
@REM -----------------------------------------------------------------------------------
    Color 0A & echo You are connected to SSID:"%SSID%"
@REM -----------------------------------------------------------------------------------
@REM Else We try we connect to the SSID
@REM -----------------------------------------------------------------------------------
    ) || (
        netsh wlan connect name="%SSID%"
    )
@REM -----------------------------------------------------------------------------------
@REM Timeout to wait 3 seconds to show the message and exit the batch script
@REM -----------------------------------------------------------------------------------
Timeout /T 3 /NoBreak>nul & Exit /B
@REM -----------------------------------------------------------------------------------

进一步阅读有关此批处理脚本中使用的命令:

相关内容