Windows 自动连接 wifi 脚本

大家好,我是空哥~

背景

电脑总是自动断开连接 WIFI,未找到原因,于是写个脚本自动连接 WIFI。

命令行连接 WIFI

查看已保存的网络连接

netsh wlan show profiles 查看 WIFI

自动连接网络

netsh wlan connect name=HUAWEI_1729

在Windows操作系统中,可以使用批处理脚本(Batch Script)或PowerShell脚本来实现定期自动连接Wi-Fi网络的功能。以下是两种方法的示例:

方法1:使用批处理脚本

  1. 打开记事本,输入以下批处理命令:

    batch

    batch 复制代码
    @echo off
    set profile=YourWiFiProfileName
    netsh wlan connect name=%profile%

    YourWiFiProfileName 替换为你的Wi-Fi网络配置文件名。

  2. 保存文件 ,命名为 connectWifi.bat

  3. 设置任务计划程序(Task Scheduler)来定期运行这个批处理脚本:

    • 打开"任务计划程序"。
    • 创建新任务,设置触发器为"周期性"。
    • 在"操作"部分,选择"启动程序",并指向你的 connectWifi.bat 文件。
    • 完成设置并保存任务。

方法2:使用PowerShell脚本

  1. 打开记事本,输入以下PowerShell命令:

    powershell

    powershell 复制代码
    $profile = "YourWiFiProfileName"
    netsh wlan connect name=$profile

    YourWiFiProfileName 替换为你的Wi-Fi网络配置文件名。

  2. 保存文件 ,命名为 connectWifi.ps1

  3. 设置任务计划程序(Task Scheduler)来定期运行这个PowerShell脚本:

    • 打开"任务计划程序"。
    • 创建新任务,设置触发器为"周期性"。
    • 在"操作"部分,选择"启动程序",并指向 powershell.exe
    • 添加参数 -ExecutionPolicy Bypass -File "C:\path\to\connectWifi.ps1"(将路径替换为你的脚本实际路径)。
    • 完成设置并保存任务。

示例

bat 脚本

SH 复制代码
@echo off
set profile=HUAWEI_1729
netsh wlan connect name=%profile%

powershell 脚本

shell 复制代码
# 使用 Invoke-Expression 执行 netsh 命令并捕获输出
$output = Invoke-Expression "netsh wlan show interface"

# 显示输出结果
#Write-Host $output

# 解析输出结果以获取特定信息
# 例如,查找 "Media Connect State" 的值
$mediaConnectState = Select-String -InputObject $output -Pattern 'HUAWEI_1729'

# 显示 "Media Connect State"
#Write-Host "Media Connect State: $mediaConnectState"

$profile='HUAWEI_1729'

# 检查Wi-Fi是否已连接
if ($mediaConnectState -eq '' -or $mediaConnectState -eq $null) {
    Write-Host "Wi-Fi is not connected. Attempting to connect..."
    

    # 连接到Wi-Fi网络
    netsh wlan connect name=$profile

    # 检查连接是否成功
    if ($wifi.InterfaceStatus -ne "Connected") {
        Write-Host "Successfully connected to $profile."
    } else {
        Write-Host "Failed to connect to $profile."
    }
} else {
    Write-Host "Wi-Fi is already connected."
}

# 显示提示信息并等待用户输入
#$userInput = Read-Host "Please enter something and press Enter"

# 输出用户输入的内容
#Write-Host "You entered: $userInput"

set-ExecutionPolicy RemoteSigned

set-executionpolicy -executionpolicy unrestricted

相关推荐
kida_yuan12 分钟前
【以太来袭】2. 节点设计与部署
后端·区块链·以太坊
渣哥38 分钟前
Spring Boot 本质揭秘:约定优于配置 + 自动装配
javascript·后端·面试
9ilk1 小时前
【同步/异步 日志系统】--- 介绍
后端·中间件
Imnobody1 小时前
吴恩达 Prompt 工程课精讲③:三次迭代,解决 Prompt 的三大致命问题(JupyterLab 实战)
后端
泉城老铁1 小时前
springboot 对接发送钉钉消息,消息内容带图片
前端·spring boot·后端
千桐科技1 小时前
qKnow 知识平台【开源版】安装与部署全指南
人工智能·后端
qq_12498707531 小时前
基于Spring Boot的高校实习实践管理系统(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
作伴1 小时前
结合Apollo手动实现一个动态线程池jar包
后端·架构
该用户已不存在1 小时前
7个没听过但绝对好用的工具
前端·后端
Java水解1 小时前
KingbaseES SQL性能调优方案分享
后端·sql