Configuration
- Windows 11
- IIS 10.0.22000.1
- PHP 5.6.40 (I know, it's old, but I don't know where my script will be executed, so I went broad)
- WSL installed
Context
I am trying to write a runLinuxCommand
that would run either on Windows (with Windows Subsystem for Linux) or on Linux (and perhaps OSX as it is Unix like).
I had some issues with exec("dir", $result, $code)
returning $code ==> 1
. I then changed the user running PHP in IIS to be my current logged in user (which is an administrator). I know, this is not ideal, but for now, I want to reach my goal. I'll try to reduce rights afterward. And... it worked!
Issue
So, I tried to execute exec("wsl date", $result, $code)
, but no luck, still an exit code of 1. Yes, opening a command prompt and executing wsl date
does work. Either with the logged in user or as administrator.
The only thing I can see for now is it takes too much time to initialize WSL, but quite not sure at all.
Attempts
Attempt #1
The one in the issue section
Attempt #2
exec('C:\\Windows\\System32\\wsl.exe date', $result, $code);
Same result.
Attempt #3
exec('start /B /WAIT C:\Windows\System32\wsl.exe date', $result, $code);
Hanging... never getting through this line.
Attempt #4
exec('start /B /WAIT cmd /C C:\\Windows\\System32\\wsl.exe date', $result, $code);
No error. No result / output.
Attempt #5
$WshShell = new \com("WScript.Shell");
$oExec = $WshShell->Exec("C:\\Windows\\System32\\wsl.exe date");
$out = $oExec->StdOut->ReadLine();
Now I have a specific error message when running the second line: File not found
. I can assure you that C:\Windows\System32\wsl.exe
exists.
I already confirmed using $oExec = $WshShell->Run("whoami", 0 , true);
as second line that the user is the one I am logged in (an administrator account from which I can test using a terminal).
Aside
I searched a lot about it, and I found nothing (well, everything I found was not specific).