0

Running this Python code in Windows 7 from an administrator account perfectly works:

import os
os.system('netsh interface set interface "Ethernet" enable')

Running the same code in Windows 10 from an admin account gives this error:

The requested operation requires elevation (Run as administrator)

How to make this work on Windows 10?

Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

2

You can use win32com.shell.shell module:

import win32com.shell.shell as shell
commands = 'interface set interface "Ethernet" enable'
shell.ShellExecuteEx(lpVerb='runas', lpFile='netsh.exe', lpParameters=commands)

If you have problems to import this module see here

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Thanks, I will try this! PS: more generally, is that *always* such a struggle each time we want to run a command requiring high privilege in Windows 10, even if UAC is set to lower position and if the current user is *already* administator? Isn't there an easier solution? – Basj Oct 16 '20 at 16:44
  • @Basj it is to struggle unless you use the built-in Administrator account – Wasif Oct 16 '20 at 16:46
  • What do you mean exactly by the built-in Adminstrator account @WasifHasan? How to set up this? I'm using the computer with a single user which is administrator (I'm the only user of the computer). Are you speaking about something else than this? – Basj Oct 16 '20 at 16:54
  • 1
    In cmd, run `net user Administrator /active:yes` and use that account. – Wasif Oct 16 '20 at 16:56
  • Using the Administrator account (RID 500) as a developer is just asking for problems. You'll end up making assumptions that something should work that's not allowed for a standard user. UAC is supposed to nag developers and make them think twice about requiring that the user has administrator access (e.g. to modify a file installed in `%ProgramFiles%`). – Eryk Sun Oct 16 '20 at 17:26
  • @WasifHasan Waw, this trick is excellent! Is this mode documented somewhere? Would you have a link or name for this "trick"? I just tried, and everything is simpler! I'll use this on one specific computer. On another one, as mentioned by ErykSun I'll keep the normal setting, because as a developer, we need to know what the end-user can/cannot do. – Basj Oct 16 '20 at 17:37
  • @ErykSun Could you post another solution without cmd.exe, or give more details about this alternative solution? Thank you in advance. – Basj Oct 16 '20 at 17:38
  • For future reference, can you post a solution or edit this answer (I believe Wasif will agree because it's relevant) to include this solution inside? – Basj Oct 16 '20 at 17:46
  • @ErykSun,Basj I agree. Edited the answer. – Wasif Oct 17 '20 at 03:37