在Powershell上执行php脚本


Executing php script on powershell

美好的一天!

我有一个Powershell代码,我想在最后运行一个php脚本。我尝试过寻找解决方案,但我似乎无法碰到任何解决方案。我所能找到的就是通过批处理文件运行 php 脚本。使用Powershell运行php脚本,这可能吗?如果是这样,如何?

使用调用运算符 ( & ):

# 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

我尝试使用上面的 R. Jessens @Mathias答案,但它对我不起作用,原因是$PhpArgs = '-f "{0}"' -f $PhpFile包含第一'-f'部分。所以使用他的答案(对我有用的是)

# 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

希望它能帮助某人:)