前言 #
因为在单位有自动启动热点的需求,所以查找到了这个办法。
准备工作 #
- Windows10
操作步骤 #
- 执行策略更改
在Windows PowerShell的管理员窗口输入
set-executionpolicy remotesigned
之后选a。这一步可以保证我们的自定义脚本正常执行。
- 创建策略
在资源管理器输入
%appdata%\Microsoft\Windows\Start Menu\Programs\Startup
打开后在该地址新建一个文本,编辑,输入
powershell -executionpolicy remotesigned -file "%appdata%\Microsoft\Windows\Start Menu\Programs\pondsihotspot.ps1"
保存更名为wifi.bat
3.创建脚本
在资源管理器输入
%appdata%\Microsoft\Windows\Start Menu\Programs
新建文本后输入
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.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() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
$netTask = $asTask.Invoke($null, @($WinRtAction))
$netTask.Wait(-1) | Out-Null
}
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
if ($tetheringManager.TetheringOperationalState -eq 1) {
""
}
else{
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
之后改名为pondsihotspot.ps1,重启后即可在10秒左右自动打开热点。
总结 #
感觉10秒左右的时间还是太长了,没有集成成系统设置的一项功能是一种遗憾。
参考链接 #
- https://zhuanlan.zhihu.com/p/496667040
- https://answers.microsoft.com/zh-hans/windows/forum/all/%E5%A6%82%E4%BD%95%E5%BC%80%E6%9C%BA%E8%87%AA/d3671fd2-b17a-4f1c-a500-13120704ea98
