Windows 11 实现移动热点自启动

原文链接:https://blog.iyatt.com/?p=16784

背景

公司的 WiFi 用起来很卡,但是电脑有线使用网络又是正常的,另外我用的那台台式机是有无线网卡的,平时就是用电脑开热点给手机用,但是每次开机都要自己手动启动就麻烦,有时候还忘了。所以到处查找方法尝试,终于找到一个可行的方案。

测试环境:
Windows 11 专业工作站版 23H2
PowerShell 5.1(使用预装的版本)

操作

设置好热点信息

{Win}+{I}打开设置,前往网络和Internet-》移动热点
file

主要就是设置网络名称和密码,频段根据需要自己选择
file

然后打开移动热点,可以看到多出来一个节能,把它关掉就不会自动停用 WiFi 了
file

允许 PowerShell 脚本执行

Windows 默认禁止 PowerShell 运行脚本文件,
以管理员身份打开
file

执行

Set-Executionpolicy RemoteSigned

file

创建 PowerShell 脚本文件用于实现 WiFi 打开

脚本基于 https://gist.github.com/primaryobjects/8b54f7f4219960127f1f620116315a37
修改。
创建一个文本文件,将下面的内容粘粘进去,保存后扩展名改为 .ps1

$PSVersionTable
"-" * 100

Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]

Function Await($WinRtTask, $ResultType) {
    $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
    $netTask = $asTask.Invoke($null, @($WinRtTask))
    $netTask.Wait(-1) | Out-Null
    $netTask.Result
}

Function AwaitAction($WinRtAction) {
    $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
    $netTask = $asTask.Invoke($null, @($WinRtAction))
    $netTask.Wait(-1) | Out-Null
}

Function Get_TetheringManager() {
    $connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
    $tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
    return $tetheringManager;
}

Function SetHotspot($Enable) {
    $tetheringManager = Get_TetheringManager

    if ($Enable -eq 1) {
        if ($tetheringManager.TetheringOperationalState -eq 1)
        {
            "Hotspot is already On!"
        }
        else{
            "Hotspot is off! Turning it on"
            Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
        }
    }
    else {
        if ($tetheringManager.TetheringOperationalState -eq 1)
        {
            "Hotspot is on! Turning it off"
            Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
        }
        else{
            "Hotspot is already Off!"
        }
    }
}

# Define a function to check the status of the hotspot
Function Check_HotspotStatus() {
    $tetheringManager = Get_TetheringManager
    return $tetheringManager.TetheringOperationalState -eq "Off"
}

# Define a function to start the hotspot
Function Start_Hotspot() {
    $tetheringManager = Get_TetheringManager
    Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}

Function exitCountdown($sec)
{
    for (; $sec -ge 0; --$sec)
    {
        "$sec"
        Start-Sleep -Seconds 1
    }
    exit 0
}

if ($args.Length -eq 0) {
    while (Check_HotspotStatus) {
        SetHotspot 1
        Start-Sleep -Seconds 2
        if (Check_HotspotStatus)
        {
            "Failure.Try again in 2s."
            Start-Sleep -Seconds 2
            continue
        }
        else
        {
            "Success.Exit in 10s."
            exitCountdown 10
            exit 0
        }
    }

    "Hotspot is already.Exit in 10s."
    exitCountdown 10
}
else {
    switch ($args[0]) {
        "0" {
            SetHotspot 0
            break
        }
        "1" {
            SetHotspot 1
            break
        }
        default {
            "Invalid parameter, please enter 1 to turn on hotspot, enter 0 to turn off hotspot"
            exit 1
        }
    }
}

建议放置到固定路径,比如我放置在**%HOMEPATH%**家目录下,命名为 hotspot.ps1
file

file

这个脚本提供了三种模式(注意这个脚本在 Windows 11 预装的 PowerShell 5.x 可以运行,但在自己安装的新的 7.x 无法使用)

# 用于实现自启动
powershell 【hotspot.ps1路径】

# 手动启动热点
powershell 【hotspot.ps1路径】1

# 手动关闭热点
powershell 【hotspot.ps1路径】1

创建自动执行任务

打开任务计划程序
file

创建任务
file

自己为任务命名
file

新建触发器,比如可以选择解锁时(输入密码认证进入桌面时)
file

新建操作
程序或脚本处填C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
添加参数处填上面脚本文件的所在路径,比如我这里是**%HOMEPATH%\hotspot.ps1**
file

条件栏可能会默认勾选使用交流电才启动任务(插上电源时),去掉的话不插电源默认也启用(主要针对笔记本电脑,勾选了没插电源不会执行)
file

设置完,确定即可,可以按{Win}+{L}锁定,再尝试登录验证脚本是否按预期的执行
file

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Windows 10是当前最新的操作系统,其使用方便、功能强大,而其内置的移动热点功能更是大大增强了笔记本电脑的易用性。然而,移动热点的功能只有在手动开启后才能使用,这对于一些经常使用移动热点的用户来说,可能会有些麻烦。为了方便使用,我们可以将Windows 10的移动热点功能设置为开机自启动。 要实现这个操作,首先需要知道的是Windows 10的开机自启动设置是在任务管理器中完成的。点击“Ctrl+Shift+ESC”键,打开任务管理器。在任务管理器中找到“启动”应用程序选项卡,这里列出了所有开机启动的程序。现在,我们就可以开始设置了。 首先,需要将移动热点的快捷方式添加到Windows的启动文件夹,这样Windows 10运行时就会自动启动。进入移动热点的快捷方式所在目录,将它拖动到“启动”文件夹中,或者右键将快捷方式复制到启动文件夹中。 接下来,右键单击移动热点的应用程序,选择“属性”,将“运行此程序的帐户”设置为“管理员”,这样在启动应用程序时就不会出现权限问题。 设置完毕后,重启计算机,发现移动热点已自动开启,而且在任务管理器的“启动”选项卡中也能看到该程序已启动。这样,在每次使用Windows 10的时候,移动热点都可以自动的启动,大大方便了用户的使用。 总的来说,将移动热点设置为Windows 10的自启动项目并不难,只需要设置一次即可实现长期稳定的使用。这样一来,用户可以再次使用Windows 10的时候就不必再手动开启移动热点,而是可以立即开始使用网络。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

astuv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值