Problem when issuing php exec in Yii2 controller under Windows 11

Hello,

I am asking for your help in solving a particularly irritating problem.

I am developing a project on Yii2, under Windows 11, with Wamp.

In one of my Yii2 controllers, I am using php exec to create a Windows scheduled task. Something like:

exec(“schtasks /create /tn ‘task name’ /tr ‘Windows action to run’ /ru ‘User with administrator privileges’ /rp “password” /sc once /sd ‘scheduled date’ /st ‘scheduled hour’ /rl highest /f”,$output,$retval);

Nothing happens: $output = null, $retval = 1 (so an error).

If I try to use shell_exec, $output is still null.

If I run schtasks from the command line or in Powershell, in normal mode I get an “Access denied” error, and in administrator mode it works correctly. So I assumed it was a permission issue, and tried several ways to resolve it:

  • Configuring the Apache service with ‘Allow the service to interact with the desktop’
  • Configuring the Apache service with the administrator account
  • Using runas and a pipe:

exec(“runas /user:PCname*Username* | schtasks /create /tn ‘task name’ /tr ‘Windows action to run’ /ru ‘User with administrator privileges’ /rp “password” /sc once /sd ‘scheduled date’ /st ‘scheduled hour’ /rl highest /f”,$output,$retval);

Nothing works!

I don’t see any errors in the Windows Event Viewer, and nothing very obvious in the Apache and PHP logs.

I would add that this involves redeveloping a project on Yii2/Windows 11/Wamp that has been running smoothly for years on Zend/Windows 10/Wamp. The logic is exactly the same.

Ultimately, I don’t know if this is a problem related to Windows 11 or Yii2.

Any help would be greatly appreciated.

André

That function is dangerous and very likely disabled. Check php.ini

But even bigger problem is you trying to do that. Why do you want to do that? It opens the whole can of security worms!

1 Like

Thank you for your reply.

The function is not disabled in php.ini, so that does not explain the problem.

I was not aware that it was dangerous. I think I have found a workaround.

Still, I don’t understand why it works on Windows 10/Wamp/Zend and not on Windows 11/Wamp/Yii2. And I don’t like not understanding :thinking:

May be some permission issues. Windows can sometimes be wild chase.

Glad you solved it, avoid that command like a plague