繁体   English   中英

从PHP脚本调用Powershell在Ubuntu上没有可用的命令

[英]Calling Powershell from PHP script no commands available on Ubuntu

我正在尝试从Web应用程序调用Powershell脚本。

我为Ubuntu 16.04安装了Powershell(我正在运行Ubuntu 16.10),开发了我的PS脚本,并从Powershell命令行对其进行了尝试,并且效果很好(目的是制作VMWare快照)。

我也尝试从bash shell启动,如下所示:

/usr/bin/powershell -Command "/home/myuser/path/to/powershell/script/make_snapshot.ps1 -SnapshotName \"toto snapshot\" -Description \"test powercli snapshot\" -VMName \"toto.mydomain.fr\""

而且效果也不错。

但是,当我从shell_exec();运行此完全相同的命令时shell_exec(); 在PHP中,启动Powershell时我首先遇到了问题:

'System.Management.Automation.ConfigPropertyAccessor' exception when launching PowerCLI Core

我通过此博客文章解决了该问题( http://www.virtuallyghetto.com/2017/01/system-management-automation-configpropertyaccessor-exception-when-launching-powercli-core-in-linux-firstboot-script.html ),将我的~/.local/复制到/var/www/html/.local/并指定export HOME=/var/www/html以便它在正确的Home(Apache Home)中运行,并且可以加载PowerCLI模块住在里面。

完成此操作后,我认为它将运行我的脚本,并且每个人都会感到高兴。 显然不是这样。

综上所述,我现在以这种方式调用脚本:

$ps_scripts_dir = '/home/myuser/path/to/powershell/script/';
$ps_script = "make_snapshot.ps1";
$params = '-VMName \"toto.mydomain.fr\" '.
          '-SnapshotName \"toto snapshot\" '.
          '-Description \"test powercli snapshot\"';
$runScript = $ps_scripts_dir.$ps_script;
$runCMD = "export HOME=/var/www/html && /usr/bin/powershell -Command \"";

$executed = $runCMD.$runScript." ".$params."\" 2>&1";

$output = shell_exec($executed);

echo "<pre>";
print_r($output);
print_r($executed);
echo "</pre>";

而且似乎正确调用了Powershell,但是即使我将Powershell脚本简化为简单的Write-Host "aaaaa"或什至只有Get-Command ,似乎也没有Cmdlet可用。

这是输出:

<pre>
export HOME=/var/www/html && /usr/bin/powershell -Command "/home/myuser/path/to/powershell/script/make_snapshot.ps1 -VMName \"toto.mydomain.fr\" -SnapshotName \"CMDB TEST test test\" -Description \"Fait depuis la CMBD par admin le 06-04-2017 à 09-05-51 Test dune snapshot depuis AOR vers Lumen vers Powercli vers VCSA\"" 2>&1

get-command : The type initializer for 
'System.Management.Automation.AnalysisCache' threw an exception.
At /home/remi/Documents/Restful-API-CMDB/lumen_rest_api/powercli_scripts/make_s
napshot.ps1:2 char:1
+ get-command
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Command], TypeInitializat 
   ionException
    + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.Pow 
   erShell.Commands.GetCommandCommand

write-host : The term 'write-host' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, or 
if a path was included, verify that the path is correct and try again.
At /home/remi/Documents/Restful-API-CMDB/lumen_rest_api/powercli_scripts/make_s
napshot.ps1:3 char:1
+ write-host "aaaaa"
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (write-host:String) [], CommandN 
   otFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

</pre>

因此,似乎未加载某些内容或某些配置未正确完成。 但是我找不到。

我尝试了/usr/bin/powershell -ExecutionPolicy RemoteSigned -Command "[...]"但是存在一个已知的错误,该错误使该命令失败并出现Aborted (core dumped)错误。 https://github.com/PowerShell/PowerShell/blob/master/docs/KNOWNISSUES.md#user-content-command-availability

可能会发生什么?

我正在使用powershell_6.0.0-alpha.15-1ubuntu1.16.04.1_amd64.deb

升级到powershell_6.0.0-alpha.17-1ubuntu1.16.04.1_amd64.deb解决了该问题。

感谢Powershell团队的MaximoTrinidad指导我: https//github.com/PowerShell/PowerShell/issues/3494

暂无
暂无

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

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