【问题标题】:Can't run powershell script from php无法从 php 运行 powershell 脚本
【发布时间】: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


【解决方案1】:

正如 jisaak 所说,问题是我的网络服务器无法访问目标目录。

通过正确地对路径进行硬编码,问题已得到解决。

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 2013-01-02
    • 2020-12-05
    • 2021-05-13
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多