1

I have a script that removes and substitute some files in C:\Windows directory.

I start command line as admin then I start my python script in it. And when the script tries to remove files from C:\Windows, I get WindowsError error 5.

How can I fix this?

Possible solution: Actually I was trying to modify files which has all privileges only for TrustedInstaller user, so I used this https://github.com/jschicht/RunAsTI to run python script.

GALIAF95
  • 479
  • 1
  • 7
  • 21
  • Did you try using "runas" cmd command? – iColdBeZero Aug 12 '17 at 09:53
  • You're an administrator, so you can take ownership and grant yourself whatever permissions you want. You can use `subprocess.call` to run takeown.exe and icacls.exe. – Eryk Sun Aug 13 '17 at 02:33
  • answer at https://stackoverflow.com/questions/19672352/how-to-run-python-script-with-elevated-privilege-on-windows –  Nov 15 '19 at 05:39

1 Answers1

1

WindowsError error 5 occurs when you have no System Administrator privileges to perform action.
You can try to force script to run with admin priveleges with Windows cmd command runas.
Try something like this:

runas /user:administrator_account path_to_script

Just replace administrator_account with account name that has privileges on your computer. Also this command will prompt for password (if account has one setup). For more information about this command you can read here.

iColdBeZero
  • 255
  • 2
  • 11
  • 2
    With the default UAC and policy settings nowadays, runas.exe will only work the way you expect with the "Administrator" account (RID 500), which has to be manually enabled. Other administrator accounts are logged on with a split token, and runas.exe will create the process using the standard token instead of the elevated token. – Eryk Sun Aug 12 '17 at 10:20
  • But the OP claims to be working from an elevated command prompt already, so Python should already have admin rights, in which case the OP may be trying to delete a memory-mapped file, such as a loaded DLL, running executable, or mapped data file. The memory manager doesn't allow deleting memory-mapped files. – Eryk Sun Aug 12 '17 at 10:23
  • You are totally right, forgot about that thanks for note – iColdBeZero Aug 12 '17 at 11:23
  • Thank you for answers, but I found that files that I need to modify has all privileges only for TrustedInstaller (I updated my question) – GALIAF95 Aug 12 '17 at 17:56