'Stop / Start network in autohotkey without needing to be an administrator
I'm trying to map out via AutoHotkey a keyboard shortcut to enable / disable windows networking.
I found online this solution:
ConnectedToTheInternet(flag=0x40) {
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag, "Int", 0)
}
+T::
adapter := "Local Area Connection" ; Adapter Name
if ConnectedToTheInternet()
runwait,netsh interface set interface "%adapter%" admin=disabled,,hide
else
runwait,netsh interface set interface "%adapter%" admin=enabled,,hide
return
return
Using Shift + T as test hotkey, it seems to work but only if is in Admin mode otherwise netsh dosn't work. Instead of running AutoHotkey script in admin mode, if I go in control panel and right click on the network interface disabling it, it just disable/enable it without asking any admin privileges.
There's something else that I can use instead of netsh in order to bring down/up network interfaces so that I could not need the administrative privileges ?
EDIT: I'm a bit disappointed. I'm considering to write a routine to open the Network and Sharing center (see pic below) and automatically click to enable/disable the interface and close automatically the window but would be not so nice to see each time.
Otherwise I would have to write a service listening on a socket only to being interfaced with my script, a simple c# service that do only the wmic or netsh command with admin privileges without having to privilege with admin the whole AutoHotkey script...
Solution 1:[1]
Use WMIC.
Enable interface:
wmic path win32_networkadapter where "netconnectionid like '%Local Area Connection%'" call enable
Disable interface:
wmic path win32_networkadapter where "netconnectionid like '%Local Area Connection%'" call disable
You can type directly these commands in command prompt. Integrate them in AutoHotKeys is then trivial.
Reference: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapter
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wisblade |

