请问 Win11 手动关闭无线网络后如何通过命令行开启

3 天前
 ms2297248353

参考提问:Windows11 手动关闭无线网络后无法通过命令行开启


PS C:\Users\ms> Get-NetAdapter   

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed   

----                      --------------------                    ------- ------       ----------             ---------  

蓝牙网络连接 2            Bluetooth Device (Personal Area Ne...#2      29 Disconnected 34-6F-24-99-2A-C8         3 Mbps  

以太网                    Realtek PCIe GbE Family Controller           28 Disconnected 50-EB-F6-30-9F-A1          0 bps  

WLAN                      MediaTek Wi-Fi 6 MT7921 Wireless LAN...      26 Up           14-21-E1-93-A9-4F       1.2 Gbps 

手动关闭 WLAN 后通过终端执行:

没有响应


Enable-NetAdapter -Name "WLAN" -Confirm:$false 

提示网络不存在


netsh interface set interface name="WLAN" admin=enable 

运行日志


PS C:\Users\ms> netsh interface set interface name="WLAN" admin=enable  

此网络连接不存在。 

什么逻辑呢,WLAN 开启状态下,打开终端管理员,可以通过命令,启动和关闭命令。

netsh interface set interface name="WLAN" admin=disable
netsh interface set interface name="WLAN" admin=enable
Disable-NetAdapter -Name "WLAN" -Confirm:$false
Enable-NetAdapter -Name "WLAN" -Confirm:$false

以上命令在无线网络开启状态下正常启动和关闭 wifi

但是手动关闭 WLAN 后

就不能同命令行开启 WLAN

希望有技术的大佬,交流交流经验。 感谢大佬了~

284 次点击
所在节点    问与答
4 条回复
NoOneNoBody
3 天前
netsh wlan connect 参数?
没这样玩过,你需求是程序启动?
kokutou
3 天前
没找到 我怀疑是 gui 显示
因为你关了马上开,ssid 还是那些啊,感觉跟没关一样。。。
redtears
3 天前
https://learn.microsoft.com/en-us/answers/questions/2074409/resolved-how-to-toggle-wi-fi-in-windows-11-via-the
```shell
[CmdletBinding()] Param (
[Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$Status
)

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
}
[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$wifiRadio = $radios | ? { $_.Kind -eq 'WiFi' }
[Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ($wifiRadio.SetStateAsync($Status)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
```
以上代码保存为 ToggleWifiRadio.ps1
# Turn off
PS D:\path\to\your\folder> .\ToggleWifiRadio.ps1 -Status 'Off'

# Turn on
PS D:\path\to\your\folder> .\ToggleWifiRadio.ps1 -Status 'On'
ms2297248353
3 天前
@redtears 感谢大佬,雀实可行

还是不会检索,网上找了一圈都没找到🙈

十分感谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://tanronggui.xyz/t/1110280

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX