PowerShell上的输出流用于SSH调用

时间:2014-06-07 04:44:12

标签: php powershell ssh

我的PHP脚本(在Linux中)没有在屏幕上打印我的值,但它会打印一条警告消息。

e.g。

这是我的PHP代码:

$stream = ssh2_exec($connection, 'powershell "c:\remoteScript.ps1" -$NotificationID "5359f76f888ddf35b889dedf"');
$string = stream_get_contents($stream);
echo "Powershell output: ". $string;

这是我的powershell代码:

Write-Warning "This is a test"

这就是PHP的写作:

  

Powershell输出:警告:您应该将PowerShell更新为   PowerShell 2.0版本。

问题:如何捕获Powershell返回的流?

1 个答案:

答案 0 :(得分:0)

解决方案:我们需要告诉ssh2_exec是否等待/阻止,直到远程进程完成search for "block" on this page to see an example

$stream = ssh2_exec($connection, 'powershell \'C:\scripts\remoteVeeamClientSummary.ps1\' -Client \''. $Client. '\' -NotificationID \''. $NotificationID. '\'');

stream_set_blocking($stream, true);

return stream_get_contents($stream);

和powershell:

Write-Output $result