0

Good Day!

I have a powershell code and I would like to run a php script at the very end. I have tried searching for solutions but I can't seem to bump on any. All I could find is to run php script through a batch file. Running a php script using powershell, Is this possible? If so, how?

lukemems_2
  • 125
  • 1
  • 2
  • 9

2 Answers2

1

Use the call operator (&):

# Set up references to executable and script
$PhpExe  = "C:\path\to\php\install\dir\php.exe"
$PhpFile = "C:\path\to\script.php"

# Create arguments from Script location
# usually php.exe is invoked from console like: 
# php.exe -f "C:\path\myscript.php"
$PhpArgs = '-f "{0}"' -f $PhpFile

# Invoke, using the call operator
$PhpOutput = & $PhpExe $PhpArgs
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
1

I tried using @Mathias R. Jessens answer above but it wouldn't work for me, the reason was the $PhpArgs = '-f "{0}"' -f $PhpFile contained the first '-f' part. So using his answer (and what worked for me was)

# Set up references to executable and script
$PhpExe  = "C:\path\to\php\install\dir\php.exe"
$PhpFile = "C:\path\to\script.php"

# Create arguments from Script location
# usually php.exe is invoked from console like: 
# php.exe -f "C:\path\myscript.php"
$PhpArgs = '"{0}"' -f $PhpFile //Changed this line!

# Invoke, using the call operator
$PhpOutput = & $PhpExe $PhpArgs

Hope it helps someone :)

Tamazin
  • 103
  • 2
  • 13