1

When I start

os.system('"runas /user:USERNAME "C:/Python27/python.exe shell.py""')

it iterates over and over in Console.

I need to start a program from computer owner to elevate the rights, since program will be used on Win7, which leads to care about UAC.

I decided to go in a way that to start the program inside, but with elevated rights and exit immediately.

I used this code

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) 

` But in my case nothig starts with ShellExecuteEx. And instead of ShellExecuteEx I put os.system using runas command, which iterated in Console string like runas /user:USERNAME "C:/Python27/python.exe shell.py" over and over.

import os
import sys
import win32com.shell.shell as sh    ASADMIN = '/user:@'
os.system('"runas /user:=@COMPANY_NAME "C:/Python27/python.exe shell.py""')
sys.exit(0)
if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([ASADMIN] + ['c:\\Python27\\python.exe', script] + sys.argv[1:])
    sh.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    sys.exit(0)

This is my code.

John
  • 143
  • 1
  • 2
  • 12
  • 1
    You might find the [accepted answer to this question](http://stackoverflow.com/q/130763/62576) useful. – Ken White Aug 24 '12 at 16:24
  • As I see, there is a variant I used. However, command os.system('"runas /user:USERNAME "C:/Python27/python.exe shell.py""') iterates over and over in Console without starting runas program, how to fix it? – John Aug 25 '12 at 14:39
  • If you read the link I posted (the accepted answer), you'll see you can't elevate in the way you're doing. There's a working sollution in that post. It doesn't matter if you say "I'm doing something totally different kind of like that and it's not working". You've posted one line of code, and a vague "it iterates over and over in Console". The line of code you've posted can't "iterate" at all, but you're not showing any more code or giving any more details. I don't know how you expect to get help when you don't give us info and don't read solutions suggested. ? – Ken White Aug 25 '12 at 15:27
  • I actually made similar code to this: 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) But in my case program exits with zero and nothig starts with ShellExecuteEx. And instead of ShellExecuteEx I put os.system using runas command, which iterated in Console string like runas /user:USERNAME "C:/Python27/python.exe shell.py" over and over – John Aug 25 '12 at 18:15
  • 1
    @John - why not just copy in the exact code you are using? – selllikesybok Aug 27 '12 at 20:24

0 Answers0