IcanCwhatUsay
IcanCwhatUsay

Reputation: 81

How to open command prompt in Administrator mode in python?

I'm a bit of a noob, so sorry in advance if I'm not doing this right.

I'm using python 3.6x

What I have so far that gets the command window open is

import os
os.open("start cmd")

but this comes up in the directory that I'm working from and not in administration mode.

I've also tried

import os
os.system("start /wait cmd /wait {tskill dwm}")

but that didn't work either. (tskill dwm is what I'm trying to get python to write in to command manager to fix a bug with windows' buttons not going away)

My overall goal is to just click this python script Blah.py and have it restart the windows viewer or whatever it's called. Doing this clears the stuck buttons. Overall this is just an exercise in practicing python. I know I could just disable the button fade out and that would take care of the issue. I just figured this would be a good learning opportunity.

Upvotes: 2

Views: 17086

Answers (2)

Psysmar
Psysmar

Reputation: 31

from pynput.keyboard import Key, Controller
import time
keyboard = Controller()

keyboard.press(Key.cmd)
keyboard.release(Key.cmd)
time.sleep(0.3)

keyboard.type("cmd")
time.sleep(1)
keyboard.press(Key.right)
keyboard.release(Key.right)
keyboard.press(Key.down)
keyboard.release(Key.down)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(0.5)
keyboard.press(Key.tab)
keyboard.release(Key.tab)
keyboard.press(Key.tab)
keyboard.release(Key.tab)
keyboard.press(Key.enter)
keyboard.release(Key.enter)

exit

Upvotes: 1

Narendra
Narendra

Reputation: 1529

The answer is here

https://stackoverflow.com/a/11746382/7352806

import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
    shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    sys.exit(0)

Upvotes: 1

Related Questions