Windows通过命令行开启热点

要求: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])
}

参考:

通过 命令行PowerShell 开启、关闭 Windows 10 移动热点,可修改SSID、密码。解决神州网信政府版win10无法管理windows自带移动热点问题。_win10命令行下删除热点-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值