Search code examples
phpiistaskexecwindows-task-scheduler

Access denied when using schtasks from PHP (IIS) on windows server


I'm trying to run schtasks with exec() but always get an access denied error as response.

On my local win10 machine it works seamlessly but on windows server 2019 the scrip exits with the error. I found serveral similar questions but wether they had no answeer or the given answer didn't work for my specific problem. Unfortunately the error doesn't specify WHO is missing permition to WHAT. So i tryed giving the IUSR and the IIS_USERS access to the cmd.exe, the schtasks.exe and the taks folder (C:\Windows\System32\Tasks) according to some other answers.

My code example:

<?php
$results = array(
    "output" => NULL,
    "code" => NULL
);
exec(
    'schtasks /create /sc MONTHLY /tn AtlantisPrint /tr C:\SoftwarehausHeider\Atlantis\prog\atlantis.exe /ru Administrator /rp XXXX /f 2>&1',
    $results["output"],
    $results["code"]
);
echo "<pre>" . print_r($results, TRUE) . "</pre>";

Results in:

Array
(
    [output] => Array
        (
            [0] => Zugriff verweigert // translates to *Access denied*
        )

    [code] => 1
)

If i run the command in the command line it works. I also tryed to use runas /user:IUSR with the command but it results in a request to enter the password for IUSR which i know nor how to get it.

It's PHP 7.4 on an IIS 10.0 in case that this is relevant.

I'm thankful for any suggestion which permissions are missing here.

Edit (2021-10-26):

Other things i tried by now are deactivating the UAC, removing the write protection from the task folder and deactivating the firewall. But it still says 'Access denied'.


Solution

  • Eventually i could find the missing permission with some help. The IUSR was missing access to the C:\Windows\ SysWOW64 \schtasks.exe but i tried giving it access to the C:\Windows\ System32 \schtasks.exe before. So after giving the IUSR read and execute privileges for the C:\Windows\ SysWOW64 \schtasks.exe everything works as expected.