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,393

6 Answers6

5

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
  • 179
3

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)
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
  • 738
1

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

1

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?