无忧启动论坛

标题: WLAN开关用命令行怎么开启或关闭 [打印本页]

作者: 1G空间100元    时间: 2024-12-2 15:19
标题: WLAN开关用命令行怎么开启或关闭
本帖最后由 1G空间100元 于 2024-12-2 20:20 编辑



在设置--网络和internet--WLAN--开或关
如果用命令行怎么开关呢?(不是禁用网卡)

以下是切换无线网络和有线网络(禁用网卡方法)的代码:
(但需要的不是这种效果,只是想要单纯的控制: 设置--网络和internet--WLAN--开 或者 关)
  1. @echo off
  2. setlocal
  3.   
  4. :: 设置网卡名称
  5. set WiredInterfaceName="以太网"
  6. set WirelessInterfaceName="WLAN"
  7.   
  8. :: 检查当前状态并切换
  9. netsh interface show interface "%WiredInterfaceName%" | findstr /C:"已启用" > nul
  10. :: 这里的findstr命令用于在netsh interface show interface命令输出的关于有线网卡接口信息中查找是否包含"已启用"这个字符串。
  11. :: 将结果重定向到nul(空设备),这样就不会在屏幕上显示查找过程中的信息。
  12. :: ERRORLEVEL是一个系统变量,它存储了上一个命令(这里是findstr命令)的退出状态码。如果findstr找到了匹配的字符串(即网卡已启用),则ERRORLEVEL为0,否则为非0。
  13. if %ERRORLEVEL% equ 0 (
  14.     echo 禁用有线网卡...
  15.     netsh interface set interface "%WiredInterfaceName%" admin=disable
  16.     echo 启用无线网卡...
  17.     netsh interface set interface "%WirelessInterfaceName%" admin=enable
  18. ) else (
  19.     echo 启用有线网卡...
  20.     netsh interface set interface "%WiredInterfaceName%" admin=enable
  21.     echo 禁用无线网卡...
  22.     netsh interface set interface "%WirelessInterfaceName%" admin=disable
  23. )
  24.   
  25. :: 添加错误处理部分,当netsh命令执行失败时给出提示
  26. if not %ERRORLEVEL% equ 0 (
  27.     echo netsh命令执行失败,请检查网卡名称或网络连接状况。
  28. )
  29.   
  30. echo.
  31. echo 切换完成。
  32. pause
  33. endlocal
复制代码









作者: wang1126    时间: 2024-12-2 15:30
本帖最后由 wang1126 于 2024-12-2 15:32 编辑

1. 列出所有网络适配器
netsh interface show interface
2. 禁用网络适配器: 根据适配器名称(例如 "Ethernet" 或 "Wi-Fi"),使用以下命令禁用对应的网络适配器:
netsh interface set interface "Wi-Fi" admin=disable
netsh interface set interface "Ethernet" admin=disable

开启则将disable改成 enable

作者: 假伪君子    时间: 2024-12-2 16:02
1111
作者: wn168cn@163.com    时间: 2024-12-2 17:34
感谢2#分享
作者: nianyueriPE    时间: 2024-12-2 18:02
谢谢分享
作者: w96692    时间: 2024-12-2 18:36
感谢分享
作者: kukuyu    时间: 2024-12-2 18:45
感谢分享
作者: slore    时间: 2024-12-2 23:56
试试 net start/stop 无线网络服务
作者: 喜欢与撩    时间: 2024-12-3 00:40
有没有可能你说的设置里开或关本质上就是禁用网卡或者禁用WIFI服务呢?你可以设置上把WIFI关了后用CMD查看网卡是否保持开启状态就可以知到它的原理了,不过其实无论通过啥形式实现,能达到目标的都是好方法
作者: 2012fengxi    时间: 2024-12-3 07:58
解决了没有
作者: baby1277    时间: 2024-12-3 09:50
本帖最后由 baby1277 于 2024-12-3 09:59 编辑

重启系统后生效,亲测成功。

关闭:
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0000" /v "radioEnable" /t REG_DWORD /d 0 /f  
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0000" /v "radioEnable" /t REG_DWORD /d 0 /f  
打开:
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0000" /v "radioEnable" /t REG_DWORD /d 1 /f  
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0000" /v "radioEnable" /t REG_DWORD /d 1 /f  






作者: baby1277    时间: 2024-12-3 09:52
方法:用Regshot对比注册表快照。
作者: softwarezheng1    时间: 2024-12-3 10:09
这个代码对PC机还是手机平板的呢
作者: 无赖宵小    时间: 2024-12-3 10:36
感谢分享,记录一下
作者: guong    时间: 2024-12-3 10:46
来学习了
作者: 2012fengxi    时间: 2024-12-3 23:01
感谢分享

作者: wahmlt321    时间: 2024-12-4 06:35

感谢分享
作者: 孟仁飞    时间: 2024-12-6 14:41
学习
作者: 远大的理想    时间: 2024-12-7 09:42
谢谢分享
作者: ALADO    时间: 2025-4-13 02:26
本帖最后由 ALADO 于 2025-4-13 02:35 编辑

之前我用注册表对比过 主要是这两项发生了变化,但运行后没有用
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0010" /v "SwRadioState" /t REG_DWORD /d 1 /f    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0010" /v "SwRadioState" /t REG_DWORD /d 1 /f   reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0010" /v "SoftwareRadioOff" /t REG_DWORD /d 0 /f    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0010" /v "SoftwareRadioOff" /t REG_DWORD /d 0 /f  
作者: ALADO    时间: 2025-4-13 02:31
重发一下 之前的网路不好 格式又有问题

我想我下面的代码会是你想要的 里面有代码,可以的话就帮我点赞 可惜我的csdn博客里有讲代码,可惜不能发送uri, 如果你想看 可以 在csdn中搜索“windows powershell 切换wifi开关“关键词来搜索

如何使用:保存为test.ps1, 以管理员身份打开powershell,运行 powershell -ExecutionPolicy bypass -File "存放路径\test.ps1" -WlanStatus On/Off,最后的On就是打开,Off就是关闭

代码如下

[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$WlanStatus
)

# 确保 WLAN 服务正在运行
If ((Get-Service wlansvc).Status -eq 'Stopped') { Start-Service wlansvc }

# 加载 Windows Runtime 组件
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
}

# 加载必要的 WinRT 类型
[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]])

$wlan = $radios | ? { $_.Kind -eq 'Wifi' }
Await ($wlan.SetStateAsync($WlanStatus))  ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
作者: wind315    时间: 2025-4-14 12:29
进来学习
作者: zyy    时间: 2025-4-20 12:44
谢谢分享,学习学习




欢迎光临 无忧启动论坛 (http://bbs.wuyou.net/) Powered by Discuz! X3.3