V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ms2297248353
V2EX  ›  问与答

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

  •  
  •   ms2297248353 · 3 天前 · 281 次点击

    参考提问: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

    就不能同命令行开启 WLAN

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

    4 条回复    2025-02-10 15:13:16 +08:00
    NoOneNoBody
        1
    NoOneNoBody  
       3 天前
    netsh wlan connect 参数?
    没这样玩过,你需求是程序启动?
    kokutou
        2
    kokutou  
       3 天前 via Android
    没找到 我怀疑是 gui 显示
    因为你关了马上开,ssid 还是那些啊,感觉跟没关一样。。。
    redtears
        3
    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
        4
    ms2297248353  
    OP
       3 天前
    @redtears 感谢大佬,雀实可行

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

    十分感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5523 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:35 · PVG 16:35 · LAX 00:35 · JFK 03:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.