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

相关推荐
小王不爱笑13213 小时前
Spring AOP(AOP+JDBC 模板 + 转账案例)
java·后端·spring
Python私教13 小时前
Rust基本语法
后端
Python私教13 小时前
Rust环境搭建
后端
jakeswang13 小时前
ServletLess架构简介
java·后端·servletless
夕颜11114 小时前
如何让 AI 按照你的预期输出
后端
q***563814 小时前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端
q***575015 小时前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
wuxuanok15 小时前
Debug —— 部署微服务中微服务的配置文件及本地注册Nacos没问题但部署到Docker中就注册失败
docker·微服务·架构
猪猪拆迁队16 小时前
前端图形引擎架构设计:双引擎架构设计
前端·后端·架构
GISer_Jing16 小时前
Node.js 开发实战:从入门到精通
javascript·后端·node.js