8

i have a problem with starting an .exe.exe file as administrator rights..

I also tried:

subprocess.call(['runas', '/user:Administrator', 'myfile.exe'])

But then i have to enter a password..

Is there any chance to leave that out?

Thanks!

PS: I searched now for some hours... didn't find anything!

ᔕᖺᘎᕊ
  • 6,253
  • Why would you remove the password? – Diblo Dk Jul 04 '13 at 18:25
  • You could use an stdin PIPE to send it, but then you need to store it somehow accessible to your Python script. That looks like proc = subprocess.call(['runas','/user:Administrator','myfile.exe'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) Then you could do proc.stdin.write('password\r\n'). – nerdwaller Jul 04 '13 at 18:28
  • @nerdwaller, I'm trying this for a while but without success. Are you sure this works? – McLeary Feb 11 '14 at 00:30

6 Answers6

4

It's a little roundabout, but another way is to run a shell command, launch Powershell (comes with Windows), then tell Powershell to run the .exe as Admin:

(just remember that the shell command is in CMD, so you escape with backslash, not Powershell's backtick.)

Powershell command: Start-Process "executable.exe" -ArgumentList @("Arg1", "Arg2") -Verb RunAs

CMD running Powershell: Powershell -Command "& { Start-Process \"executable.exe\" ... }"

Python running CMD runnning Powershell:
os.system(r'''
Powershell -Command "& { Start-Process \"notepad.exe\"
 -ArgumentList @(\"C:\\Windows\\System32\\drivers\\etc\\hosts\")
 -Verb RunAs } " '''
Sam
  • 169
2

This answer worked for me

import ctypes, sys

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    # Code of your program here
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
  • 1
    Great solution. I had to add an exit(0) at the end within the else case so that the program doesn't continue down and run the rest of my code. – Frak Dec 22 '18 at 04:47
2

The only way I know from what you say, is to use "Application Compatibility Toolkit" http://www.microsoft.com/downloads/details.aspx?FamilyId=24DA89E9-B581-47B0-B45E-492DD6DA2971&displaylang=en

And how to use it: https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/window-on-windows/?p=635

Source: Can you turn off UAC for a single app?

Diblo Dk
  • 729
0

I also went down this rabbit hole and found a solution that worked for me; PowerShell. My goal was to change an interface's IP from python, below is the code:

change_ip_win.bat

@echo on
setlocal

SET INTRF="Ethernet %1%"
SET IP_ADDR=%2%
SET SUBNET=255.255.255.0
SET GATEWAY=0.0.0.0

netsh interface ipv4 set address name=%INTRF% static %IP_ADDR% %SUBNET% %GATEWAY%

endlocal

ip_changer.py

import os
import subprocess

_bat_file_path = os.path.join(os.path.dirname(os.getcwd()), 'change_ip_win.bat')      # assumes .bat is in same path as this .py file
_eth_interface = str(interface_num)
_new_ip = str(ip_addr)
subprocess.check_output("Powershell -Command \"Start-Process " + _bat_file_path + " -ArgumentList \'"+ _eth_interface +"," + _new_ip +"\' -Verb RunAs\"", shell=True)

The only caveat is that this will pop up a window to confirm/authorize the change. This was acceptable for my use case

0

If you want to run .exe like mouse operating user ex. via shortcut, you can try this:

Win11: Settings > System > Developers > PowerShell > Allow local scripts execution

import os

game_exe_path = r"D:\Games\game.exe" game_dir = os.path.dirname(game_exe_path) # Assumes the game's working directory is its location.

def open_game(): # Set the working directory to the game's directory and run as admin. command = fr'Powershell -Command "&{{Set-Location -Path {game_dir}; Start-Process "{game_exe_path}" -Verb RunAs}}"' os.system(command)

open_game()

0

I realise I'm coming in late on this and it may not be a really elegant solution....but if you open an elevated command prompt and then launched your python script, would the executables called with "subprocess.call" not be launched with the same elevation as the CMD window?