How can I write a batch file to toggle my network adapters?

。_饼干妹妹 提交于 2019-12-22 06:32:03

问题


At my work, there is a company intranet I can connect to, as well as a Wifi network. But, it is company policy that we cannot connect to both networks simultaneously on one machine.

I need to use either network at different times though, and the only way I've been able to do so is by going to "Change Adapter Settings" somewhere in the Network section of Control Panel, then disabling whichever adapter I'm using (either local area network or wifi) and enabling the other one.

As a programmer the repetitiveness and tediousness of this task bothers me, so is there a script I could write to toggle these?


回答1:


You should look into netsh:

ex: netsh interface set interface "Local Area Connection" DISABLED

(or ENABLED)

create 2 scripts that do an ENABLED on the one you want and DISABLED on the other, and vice versa (or 1 script that first sees which one is up and which is down, and inverts)

This command can also list the state of the interfaces.

But it behaves differently (or uses a different syntax) between several flavors of windows... so you can also have a look at MicroSoft' devcon. Or another possibility: wmic.




回答2:


You can do this with powershell and the network adapter commandlets.

Disable-NetAdapter: PS C:> Disable-NetAdapter -Name "Ethernet 2"

Enable-NetAdapter: PS C:> Enable-NetAdapter -Name "Ethernet 2"



来源:https://stackoverflow.com/questions/14071706/how-can-i-write-a-batch-file-to-toggle-my-network-adapters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!