简体   繁体   English

无法从php运行powershell脚本

[英]Can't run powershell script from php

I use the Powershell script following to get a show run command run on a cisco device : 我使用以下Powershell脚本来获取在cisco设备上show runshow run命令:

getconfig.ps1 getconfig.ps1

$ErrorActionPreference ="Inquire"
Import-Module SSH-Sessions
$time ="$(get-date -f yyyy-MM-dd)"
$filename ="config"
$ext =".txt"
$filepath ="temp\"
$d1 ="x.x.x.x"
$u1 ="user"
$p1 ="password"
New-SshSession $d1 -Username $u1 -Password "$p1"
$c1 ="show run"
$Results = Invoke-Sshcommand -InvokeOnAll -Command "$c1" | Out-File "$filepath$filename-$time$ext"
Remove-SshSession -RemoveAll
exit

When I run this script manually by right click/run with powershell that's worlking fine. 当我通过右键单击/运行PowerShell手动运行此脚本时,这很好。 The problem come when I try to run this script via PHP. 当我尝试通过PHP运行此脚本时出现问题。

index.php 的index.php

<?php
$output = shell_exec('powershell C:\wamp\www\getconf\script\getconfig.ps1');
echo "<pre>$output</pre>";
?>

index.php loads forever and never give me an output and never create the results file. index.php永远加载,永远不会给我输出,也永远不会创建结果文件。

I have tried to run another script, like test.ps1 that simply create a directory and it's working fine; 我试图运行另一个脚本,比如test.ps1 ,只是创建一个目录,它运行正常; I get the output and my directory is created. 我得到输出,我的目录被创建。

My powershell script works. 我的powershell脚本有效。

The link between PHP and my PowerShell script works. PHP和我的PowerShell脚本之间的链接有效。

I do not understand where is the problem from. 我不明白问题出在哪里。

As commented by jisaak the problem was that my webserver wasn't able to reach the destination directory. 正如jisaak评论的那样,问题是我的网络服务器无法到达目标目录。

By hardcoding the path properly the problem has been fixed. 通过正确地对路径进行硬编码,问题得到了解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM