问题描述
我有一个很小的 .ps1
脚本,它可以在以太网和 WiFi 之间切换网络连接.我的脚本运行良好.我现在想知道如何在不禁用底层网络适配器的情况下关闭 WiFi.
I have a tiny .ps1
script which switches a network connection between Ethernet and WiFi. My script works well. I now want to know how to switch off WiFi without disabling the underlying network adapter.
这是我现有的脚本:
$lanState = Get-NetAdapter -Name MyEthernetPort | where status -EQ 'up'
if ( $lanState )
{
write-host 'Enabled... Disable now'
Get-NetAdapter -Name MyEthernetPort | ? status -NE disabled | Disable-NetAdapter -Confirm:$false
# TODO switch on wifi
netsh wlan connect name=SM-GUEST
}
else
{
Write-Host 'Disabled... Enable now'
Get-NetAdapter -Name MyEthernetPort | ? status -EQ disabled | Enable-NetAdapter -Confirm:$false
# TODO switch off wifi
netsh wlan disconnect
}
我找不到有关如何通过 PowerShell 关闭 WiFi 的任何指导(与单击 Windows 10 信息中心中的按钮时发生的情况相同).我发现的所有脚本只是禁用了底层适配器,这不是我想要的.
I haven't been able to find any guidance on how to switch off WiFi via PowerShell (in the same way as it would occur if you clicked the button in the Windows 10 info center). All scripts that I've found just disable the underlying adapter, which is not what I want.
有人可以建议如何从 PowerShell 访问此功能吗?
Can anybody suggest how to access this functionality from PowerShell?
推荐答案
我找不到任何以编程方式执行此操作的方法,因此我认为答案可能是(目前)并非完全可能.
I can't find any way to do this programmatically, and as a result I think the answer might be that it's (currently) not entirely possible.
我发现当您使用 Windows 操作中心的按钮关闭 Wifi 时,netsh
将界面显示为硬件开启"软件关闭":
What I have found is that when you use the button in the Windows action center to turn off Wifi, netsh
shows the interface as "Hardware On" "Software Off":
Netsh WLAN show interfaces
There is 1 interface on the system:
Name : WiFi
State : disconnected
Radio status : Hardware On
Software Off
但是,我看不到任何通过 netsh
或任何 *-netadapter
cmdlet 触发软件关闭"的功能.这可能是通过注册表中的一个开关控制的,但如果是这样,我看不到哪个.
However I can't see any ability to trigger "Software Off" via netsh
or any of the *-netadapter
cmdlets. It might be that this is controlled via a switch in the registry but if so I can't see which.
我的建议是通过使用 Disable-NetAdapter
和 Enable-NetAdapter
启用/禁用底层适配器来继续控制 wifi,或者如果您需要保持硬件启用(也许是为了重新配置它?)但希望在这样做时断开无线连接,您可以通过以下方式连接/断开网络:
My recommendation would be to just continue controlling wifi by enabling/disabling the underlying adapter with Disable-NetAdapter
and Enable-NetAdapter
or if it's the case that you need to keep the hardware enabled (perhaps in order to reconfigure it?) but want the wireless to be disconnected while you do so, you could connect/disconnect the network via:
netsh wlan disconnect
netsh wlan connect
这篇关于如何使用 PowerShell & 打开/关闭 WiFi 状态视窗 10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!