要求:windows10及以上系统
复制以下代码,保存到一个'.ps1'后缀的文件中,比如 HotspotOnOff.ps1,
然后使用power shell执行该文件(脚本)即可开启热点。
[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] | Out-Null
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)
# 检查 Windows 10 移动热点的状态
$tetheringManager.TetheringOperationalState
# 如果移动热点是开启状态,则关闭移动热点
if ($tetheringManager.TetheringOperationalState -eq 1){
# 关闭移动热点
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
# 如果移动热点是关闭状态,则设置热点名称、热点的密码,然后开启移动热点
else{
# 启动前先为移动热点设置SSID、密码
$accessPoint = $tetheringManager.GetCurrentAccessPointConfiguration()
# 为移动热点设置SSID
$accessPoint.Ssid = "X260-95"
# 为移动热点设置密码
$accessPoint.Passphrase = "asdfg12345"
$tetheringManager.ConfigureAccessPointAsync($accessPoint)
# 启动/开启移动热点
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
参考: