0

I am trying to open a cmd prompt to "run as administrator", in order to stop a service using net stop.

however nothing seems to be working, i have tried the following,

import os
import subprocess

subprocess.Popen(['runas', '/user:Administrator', '', 'cmd.exe', 'net stop "Cisco AnyConnect Secure Mobility Agent"'])

please can someone help with this

mohit
  • 5,696
  • 3
  • 24
  • 37
Simmi
  • 1
  • 5
  • See http://stackoverflow.com/questions/19672352/how-to-run-python-script-with-elevated-privilege-on-windows , hth – fiacre Mar 23 '17 at 15:46
  • The Administrator account has been disabled by default since Vista (about a decade now). Even if it's enabled, UAC policy can be configured to restrict it like any other account in the Administrators group. The way to elevate is by using a service or a program run by the task scheduler. The standard service for this is the AppInfo service, which is accessible via the shell API `ShellExecute[Ex]` using the "runas" verb. Despite its similar name, runas.exe uses the secondary logon service, which logs on a user normally instead of using the linked elevated token, i.e. runas.exe cannot elevate. – Eryk Sun Mar 23 '17 at 16:51

1 Answers1

0

I anticipate that runas is a command that is built into the console rather than its own executable and as such requires the shell=True parameter.

But I'm not in a situation to try this myself at the moment.

Keep in mind that this can be a security hazard with untrusted input. Use with caution. But hopefully you are already doing that when trying to run as admin :).

nomath4u
  • 98
  • 1
  • 6
  • yes using it to stop a service initiated for some testing purpose. i need to use this as part of the clean up – Simmi Mar 23 '17 at 16:07
  • 1
    No, it's `runas.exe`, not a built-in shell command. If it were built in, it would be in the cmd.exe shell, not the console. The console is the input buffer, screen buffer(s), and window, which is hosted by an instance of conhost.exe. – Eryk Sun Mar 23 '17 at 16:42