6

I have to copy dll files to C:\Windows\System32 folder. I get permission denied error. Is there some way to do that other than running the script as administrator?

This did not work:

import os
import sys
import shutil 
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]) 
    print "params : ", params, " script : ", script
    shell.ShellExecuteEx(lpVerb='runas', 
                         lpFile=sys.executable, 
                         lpParameters=params) 

print "I am root now." 
nvcudadll = "C:\\Users\\alankritag\\Desktop\\abc.dll" 
winpath = os.environ['WINDIR'] + "\\System32"
shutil.copyfile(nvcudadll, winpath)
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Alan.G
  • 113
  • 6
  • I used [this answer](http://stackoverflow.com/questions/19672352/how-to-run-python-script-with-elevated-privilege-on-windows) when I had your problem. Does this help you as well? – Nander Speerstra Jan 06 '16 at 10:08
  • @NanderSpeerstra I also tried that example. I only changed value of params in that code as path of my script. My script is: #!/usr/bin/python import shutil,os def main(): nvcudadll = "C:\\Users\\alankritag\\Desktop\\xyz.dll" winpath = os.environ['WINDIR'] + "\System32" try: shutil.copyfile(nvcudadll,winpath) except IOError,err: print err if __name__ == main(): main() – Alan.G Jan 06 '16 at 10:46
  • Only `NT AUTHORITY\SYSTEM`, `NT SERVICE\TrustedInstaller`, and `BUILTIN\Administrators` can modify the "System32" directory. Your access token needs to have one of these SIDs enabled. If not, elevate to administrator via `ShellExecute` "runas". You only need ctypes for this; you don't have to install PyWin32. – Eryk Sun Jan 06 '16 at 11:53
  • @eryksun This did not work : import os import sys,shutil 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]) print "parrams : ",params," script : ",script shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params) print "I am root now." nvcudadll = "C:\\Users\\alankritag\\Desktop\\abc.dll" winpath = os.environ['WINDIR'] + "\System32" shutil.copyfile(nvcudadll,winpath) – Alan.G Jan 06 '16 at 12:40

0 Answers0