I couldn’t toggle network connection either (even with admin rights) so that left me switching network adapter on and off in Device Manager.
That got boring fast so I looked up way to boil down enabling and disabling hardware into single shortcut.
Device Manager is purely graphical utility that makes it inconvenient to automate.
Luckily Microsoft also provides DevCon which is clone of Device Manager in form of command line utility.
Figure out device ID
DevCon is developer-grade utility so it is much less user friendly than average CLI stuff in Windows. To have it disable or enable device it must be provided with hardware ID of one.
I had googled this nice trick to simplify search in console output and run:
devcon find * | find "Atheros"
where Atheros is brand of my network card. This returned few cryptic lines with one looking like what I actually need:
PCI\VEN_1969&DEV_1048&SUBSYS_82261043&REV_B0\4&625283&0&00E5: Atheros L1 Gigabit Ethernet 10/100/1000Base-T Controller
Since PC isn’t really full of network cards PCI\VEN_1969 is enough for DevCon to identify device.
Splice with AutoIt
Two separate shortcuts to enable and disable still sounded troublesome (and produced flashing console windows) so I combined them in single AutoIt script:
If Ping("proxy.company.com") Then ShellExecute("devcon","disable PCI\VEN_1969","","",@SW_HIDE) Else ShellExecute("devcon","enable PCI\VEN_1969","","",@SW_HIDE) EndIf Exit
Script pings proxy server (revenge!) and according to response or lack of any uses DevCon (in hidden console window) to enable or disable network adapter.
DevCon is hardly utility for daily use but when there is need to control hardware – it is extremely powerful and easily scriptable.