2017-05-02 551 views
-2

我想通过PHP程序运行powershell。在powershell中,我必须运行命令“Get-FileHash -Algorithm sha256。\ SHYAM.jpeg”来生成哈希值。 Shyam.jpeg是位于目录C:\ wamp64 \ www \ Dedup中的文件。这是我的代码如何在PHP中运行powershell命令

<?php 
$psPath = "C:\\Windows\\SysWOW64\WindowsPowerShell\v1.0\\powershell.exe"; 
$psDIR = "C:\\wamp64\\www\\Dedup"; 
$psScript = "SHYAM.JPEG"; 
$runScript = $psDIR. $psScript; 
$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript; 
$out= shell_exec($runCMD); 
echo "<pre>"; 
print_r($out); 
echo "</pre>"; 
?> 

但它不工作。我努力使它工作。当我在PowerShell中尝试“Get-FileHash -Algorithm sha256。\ SHYAM.jpeg”命令时,它工作正常。请帮助我。

+0

在没有其他语言的情况下,在php中真的没有功能吗?在PHP中使用powershell看起来有点吓人:) –

回答

-1

用空格替换./并在Get-FileHash之前添加空格。

$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript; 

$runCMD = $psPath.' Get-FileHash -Algorithm sha256 '.$psDIR.$psScript; 
+0

它不工作 –

0

输出为C:\Windows\SysWOW64\WindowsPowerShell v1.0\powershell.exeGet-FileHash -Algorithm sha256./C:\wamp64\www\DedupSHYAM.JPEG

缺少路径PowerShell中,没有空格,错误的文件夹路径反斜杠逃逸,这不是去上班。

试着这么做:

<?php 
$psPath = "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe"; 
$fileDir = "C:\\wamp64\\www\\Dedup"; 
$fileName = "SHYAM.JPEG"; 
$runCMD = "$psPath -Command \"Get-FileHash -Algorithm SHA256 -Path '$fileDir\\$fileName' | Select-Object -ExpandProperty Hash\""; 
$out= shell_exec($runCMD); 
echo "<pre>"; 
print_r($out); 
echo "</pre>"; 
?> 

我还没有试过给exec,但至少输出一个明智的寻找命令。