4

I am starting the following script from a Windows 7 command line with administrator permissions:

import win32com.client
import time
import SendKeys
import os
from ctypes import *

shell = win32com.client.Dispatch("WScript.Shell")

os.startfile("C:\...exe")

I have also assigned the feature 'Run this programme as an administrator' to python.exe under Properties > Compatibility > Privilege Level. This did not change anything.

The programme still behaves differently when opened this way to how it behaves when I just open it via a double click on screen. Am I missing some important bit here? Will the process invoked in this way not be run as if started with administrator privileges?

Thanks for your help in advance!

Cheers -

Pat

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
pat
  • 91
  • 2
  • 2
  • 4
  • Possible duplicate of [How to run python script with elevated privilege on windows](http://stackoverflow.com/questions/19672352/how-to-run-python-script-with-elevated-privilege-on-windows) – Sergey Gornostaev Sep 01 '16 at 03:44

2 Answers2

2

I don't have access to Vista or Windows 7, but you should be able to use the runas command.

import subprocess
subprocess.call(['runas', '/user:Administrator', 'C:/my_program.exe'])
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
  • 4
    Thanks, Chinmay ... I tried this and was prompted for the administrator password by the Python shell. When I entered the correct password, it was rejected as LOGIN FAILURE. I did this several times to be sure I did not make a mistake entering my password. I really don't understand what is going on. Also, I don't understand why I would need to start the subprocess like this if I am running the script from an administrator shell. Can you help me understand this? – pat Jan 10 '11 at 11:54
  • @pat event though i'm reviving the thread a bit, the reason you couldn't login is because Administrator probably isn't the user you want to use... you should try : `'/user:[[dommainName]\\]userName'` – Xeltor Apr 16 '15 at 17:50
  • This is a useful solution, but I think it's missing the part where you write to stdin, as shown by user Back2Basics here: http://stackoverflow.com/questions/20505893/set-administrator-privileges-to-subprocess-check-call-in-python – LastTigerEyes Nov 18 '15 at 00:13
0

OK ... I figured out what the problem was. It actually had nothing to do with permissions, contrary to my initial suspicion. Sorry about that!

The reason why the application did not work correctly was because the Python script was located and invoked in another directory. For that reason, some of the application's dependencies were not referenced correctly and it couldn't find some of the files it needs to run properly. Moving the python script into the same directory as the invoked application was one way to fix that.

Sorry again for the misleading initial interpretation of what seemed to be the matter.

pat
  • 91
  • 2
  • 2
  • 4