0

I have a Python script that adds some text to the hosts file which allows administrator access only. So, what I do is I first open cmd from the Start menu on Windows 7, and then right click and go to Run as Administrator.

That works well.

However, my intent is to have that Python script run as soon as Windows start.

If there was no protected hosts file between, I would simply change the extension of the Python script from .py to .pyw and put the script in the Startup folder to have Windows execute it at startup, but in this case the script wouldn't run.

Anyone would suggest how I could do this?

Edits: The suggested duplicate doesn't solve my problem. If I point to the .py or the .pyw script in the Task Scheduler, nothing happens.

I think I should be pointing to a .cmd or a .bat file, but don't know what to write in such a file.

3

2 Answers 2

1

Simple,

  1. Open notepad
  2. Insert the following code

@echo off

start < python script path here >

exit

Note: mention the path of the .py script in the above tag

  1. Save this file as "anyname.bat" anywhere
  2. Open task scheduler
  3. Create new task
  4. enable "Run with highest privileges" token for this task
  5. Go to action tab
  6. Add the run a program trigger and select the batch file
  7. Schedule the task to run at startup
  8. Click OK
2
  • That's almost perfect except of there's still problem with the hosts file permissions. Your three lines don't run the Python script as administrator, do they? Commented May 3, 2016 at 8:19
  • @A.S No. But i have suggested to create a scheduled task with "Run with highest privileges" enabled. So that will take care of running the python script with admin rights
    – cyfrost
    Commented May 3, 2016 at 18:07
0

Answering my own question. What I did is created a .bat file as suggested by Ashok's answer:

@echo off
start C:/folder/script.pyw
exit

You should rename your Python under a .pyw extension.

Double clicking the .bat file doesn't solve the problem yet as it is not running as administrator. To fix that:

  1. Open task scheduler
  2. Create new task
  3. Select "Run with highest privileges"
  4. Go to action tab
  5. Add therun a program trigger and select the batch file
  6. Schedule the task to run at startup
  7. Click save

Done.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .