【发布时间】:2016-09-14 17:07:40
【问题描述】:
我使用以下 Powershell 脚本在 cisco 设备上运行 show run 命令:
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
当我通过右键单击/使用 powershell 手动运行此脚本时,效果很好。当我尝试通过 PHP 运行此脚本时,问题就来了。
index.php
<?php
$output = shell_exec('powershell C:\wamp\www\getconf\script\getconfig.ps1');
echo "<pre>$output</pre>";
?>
index.php 永远加载,永远不会给我输出,也永远不会创建结果文件。
我尝试运行另一个脚本,例如 test.ps1,它只是创建了一个目录并且运行良好;我得到输出并创建了我的目录。
我的 powershell 脚本有效。
PHP 和我的 PowerShell 脚本之间的链接有效。
我不明白问题出在哪里。
【问题讨论】:
-
尝试硬编码输出文件路径并确保网络服务器可以访问它。如果可行,请使用
split-path -parent $MyInvocation.MyCommand.Definition获取执行脚本的路径。 -
尝试正确地转义 $filepath ="temp\" ($filepath ="temp\\")
-
Jisaak - 你是我的英雄
标签: php powershell ssh shell-exec cisco