Execute batch script with admin rights in windows 8.1 by exec()

10,528

You can use RunAs Command. Since it will ask admin password and you are running it from web you should pipe the admin password to the command.

exec('echo adminpassword | runas /user:administrator fullPathToProgram',$output);
print_r($output);
Share:
10,528
Muhammad Arslan Jamshaid
Author by

Muhammad Arslan Jamshaid

Updated on June 04, 2022

Comments

  • Muhammad Arslan Jamshaid
    Muhammad Arslan Jamshaid almost 2 years

    I am trying to run a batch file from php with exec() but it is not working. When I run the same script in a command window in admin mode the script works.

    Now I want to know how can I configure in php that my script can be execute with ADMIN mode.

  • Harry Johnston
    Harry Johnston about 10 years
    Embedding an administrator password into a PHP script is a pretty good way to get hacked.
  • Harikrishnan
    Harikrishnan about 10 years
    @HarryJohnston Yes of course... Also Using windows and enabling dangerous php functions like exec is another good way to get hacked...:)
  • Steve Bauman
    Steve Bauman over 4 years
    @HarryJohnston Who says that the password isn't dynamically passed into the command from (administrator) user input and never stored? This is a completely fine approach.
  • Harry Johnston
    Harry Johnston over 4 years
    @Steve, I don't think that's what the OP was asking to do, but I could be mistaken. In retrospect, though, the main problem with this answer is that it won't work: runas filters the user token, so you don't get admin privilege even if you provide admin credentials.
  • Steve Bauman
    Steve Bauman over 4 years
    @HarryJohnston Good point - reading it over I think you're right. By the way, do you know of an alternative to runas? I'm trying to have PHP automatically import a scheduled task XML file under the "SYSTEM" account, but it fails due to permissions - even when using the schtasks params (/u, /p)
  • Harry Johnston
    Harry Johnston over 4 years
    @Steve, I think typically you would want to run the PHP script with elevated credentials, whether you can do that and how you would go about it depends on the scenario.