如何设置Windows11开机自启动热点

1 如何设置Windows11开机自启动移动热点功能

1.1 需求简述

  • 我的平板不能装电话卡,需要连接电脑WiFi,但每次笔记本的热点都得手动打开,很不方便,遂实现此功能。

1.2 实现思路

  • 写一个shell脚本实现启动热点的功能;
  • 写一个bat脚本调用shell脚本,放入自启动文件夹C:\Users\<你的用户名>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup中即可。

1.3 提前准备

  • 按下键盘快捷键Win + X + A,打开终端管理员,复制下面的命令,粘贴并按下回车键,确保你的PowerShell 可以运行本地创建的脚本文件
  • image.png
1
Set-Executionpolicy RemoteSigned
  • image.png

1.4 创建shell脚本

  • 在任意路径下,创建一个txt文档,将下面的内容粘贴进去,并将文件的后缀修改为.ps1。【我创建在”D:\Documents”路径下】 【🤔,这一步你需要会修改文件后缀,你会的,对吧~】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 显示PowerShell版本信息
# $PSVersionTable
# 打印100个"-"作为分隔线
# "-" * 100

# 导入必要的.NET程序集,用于处理Windows Runtime异步操作
Add-Type -AssemblyName System.Runtime.WindowsRuntime

# 获取AsTask泛型方法,用于将Windows Runtime异步操作转换为.NET Task对象
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]

# 定义一个函数,用于等待Windows Runtime异步操作完成并返回结果
Function Await($WinRtTask, $ResultType) {
# 创建特定类型的AsTask方法
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
# 将Windows Runtime异步操作转换为.NET Task对象
$netTask = $asTask.Invoke($null, @($WinRtTask))
# 等待Task完成,超时时间为-1(无限等待)
$netTask.Wait(-1) | Out-Null
# 返回Task的结果
$netTask.Result
}

# 定义一个函数,用于等待Windows Runtime异步操作完成,不返回值
Function AwaitAction($WinRtAction) {
# 获取AsTask方法,用于处理不带返回值的异步操作
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
# 将Windows Runtime异步操作转换为.NET Task对象
$netTask = $asTask.Invoke($null, @($WinRtAction))
# 等待Task完成
$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

# 如果 $Enable 为 1,则开启热点
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])
}
}
# 如果 $Enable 为 0,则关闭热点
else {
# 如果热点已经开启,则关闭热点
if ($tetheringManager.TetheringOperationalState -eq 1) {
# "Hotspot is on! Turning it off"
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
# 否则输出提示信息
else {
# "Hotspot is already Off!"
}
}
}

# 定义一个函数,用于检查热点状态
Function Check_HotspotStatus() {
$tetheringManager = Get_TetheringManager
# 返回热点是否关闭
return $tetheringManager.TetheringOperationalState -eq "Off"
}

# 定义一个函数,用于开启热点
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
}
}
}}
  • 本脚本基于Windows 11 实现移动热点自启动 – IYATT-yx 的博客修改,删去了可视化的功能,你如果需要此功能,可直接去此大佬的博客复制他的代码。
  • 此时,选中并右键你的.ps1文件,选择复制文件路径,后面需要使用,再进行下一步。
  • image.png

1.5 创建bat脚本

  • win + R 打开命令运行窗口,复制下面的命令并粘贴
  • image.png
1
shell:startup
  • 此时会自动跳转到自启动文件夹,在此文件夹下创建一个txt文件,将下面的命令粘贴进去,并将文件后缀修改为.bat
  • 注意,应将文件路径"D:\Documents\autohotspot.ps1"替换为你的shell脚本所在的路径。
  • image.png
1
PowerShell.exe -WindowStyle Hidden -File "D:\Documents\autohotspot.ps1"
  • 可自行关闭电脑热点,双击bat脚本运行,进行测试。
  • 至此,大工告成。

1.6 参考