首页  编辑  

Windows 10及以上系统中用Powershell 启动停止 WIFI 热点

Tags: /计算机文档/脚本,批处理/   Date Created:
Windows 10以上系统,不再支持HostedNetwork(承载网络),微软已经把WiFi HostedNetwork 驱动从系统中去掉了。
所以我们不能再使用下面的指令来启动WIFI热点了
netsh wlan start hostednetwork

Windows 10下要使用命令来启动热点,可以用Powershell脚本:
param (
    [string]$Ssid = "Hotspot",
    [string]$Passphrase = "12345678"
)
$profile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tm = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($profile)
if ($tm.TetheringOperationalState -eq 1) {
    echo "Stop Hotspot"
    $tm.StopTetheringAsync() | Out-Null
} else {
    echo "Start Hotsopot"
    $ap = $tm.GetCurrentAccessPointConfiguration()
    $ap.Ssid = $Ssid
    $ap.Passphrase = $Passphrase
    $tm.ConfigureAccessPointAsync($ap) | Out-Null
    $tm.StartTetheringAsync() | Out-Null
}
为了方便使用,我们可以把上面脚本保存为 wifi.ps1 ,然后用下面的命令运行:
powershell -file wifi.ps1 name password
当然这样还是不方便,我们可以把上面的代码,用Powershell ISE自带的Base64编码功能,编码成base64(不能用其他第三方工具编码,Powershell -encodedCommand 要求的Base64格式少外有些不同!):
$command = Get-Content -path wifi.ps1 -Raw
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
[convert]::ToBase64String($bytes)
如此我们得到结果:
JABwAHIAbwBmAGkAbABlACAAPQAgAFsAVwBpAG4AZABvAHcAcwAuAE4AZQB0AHcAbwByAGsAaQBuAGcALgBDAG8AbgBuAGUAYwB0AGkAdgBpAHQAeQAuAE4AZQB0AHcAbwByAGsASQBuAGYAbwByAG0AYQB0AGkAbwBuACwAVwBpAG4AZABvAHcAcwAuAE4AZQB0AHcAbwByAGsAaQBuAGcALgBDAG8AbgBuAGUAYwB0AGkAdgBpAHQAeQAsAEMAbwBuAHQAZQBuAHQAVAB5AHAAZQA9AFcAaQBuAGQAbwB3AHMAUgB1AG4AdABpAG0AZQBdADoAOgBHAGUAdABJAG4AdABlAHIAbgBlAHQAQwBvAG4AbgBlAGMAdABpAG8AbgBQAHIAbwBmAGkAbABlACgAKQANAAoAJAB0AG0AIAA9ACAAWwBXAGkAbgBkAG8AdwBzAC4ATgBlAHQAdwBvAHIAawBpAG4AZwAuAE4AZQB0AHcAbwByAGsATwBwAGUAcgBhAHQAbwByAHMALgBOAGUAdAB3AG8AcgBrAE8AcABlAHIAYQB0AG8AcgBUAGUAdABoAGUAcgBpAG4AZwBNAGEAbgBhAGcAZQByACwAVwBpAG4AZABvAHcAcwAuAE4AZQB0AHcAbwByAGsAaQBuAGcALgBOAGUAdAB3AG8AcgBrAE8AcABlAHIAYQB0AG8AcgBzACwAQwBvAG4AdABlAG4AdABUAHkAcABlAD0AVwBpAG4AZABvAHcAcwBSAHUAbgB0AGkAbQBlAF0AOgA6AEMAcgBlAGEAdABlAEYAcgBvAG0AQwBvAG4AbgBlAGMAdABpAG8AbgBQAHIAbwBmAGkAbABlACgAJABwAHIAbwBmAGkAbABlACkADQAKAGkAZgAgACgAJAB0AG0ALgBUAGUAdABoAGUAcgBpAG4AZwBPAHAAZQByAGEAdABpAG8AbgBhAGwAUwB0AGEAdABlACAALQBlAHEAIAAxACkAIAB7AA0ACgAgACAAIAAgACQAdABtAC4AUwB0AG8AcABUAGUAdABoAGUAcgBpAG4AZwBBAHMAeQBuAGMAKAApACAAfAAgAE8AdQB0AC0ATgB1AGwAbAANAAoAfQAgAGUAbABzAGUAIAB7AA0ACgAgACAAIAAgACQAdABtAC4AUwB0AGEAcgB0AFQAZQB0AGgAZQByAGkAbgBnAEEAcwB5AG4AYwAoACkAIAB8ACAATwB1AHQALQBOAHUAbABsAA0ACgB9AA==

然后我们可以用下面的方式调用,省去临时文件的麻烦:
powershell -encodedCommand JABwAHIAbwBmAGkAb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