简体   繁体   English

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

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

I am trying to call a Powershell script from a webapp. 我正在尝试从Web应用程序调用Powershell脚本。

I installed Powershell for Ubuntu 16.04 (I am running Ubuntu 16.10), developped my PS script, tried it from Powershell command line and it worked fine (the purpose is to make VMWare snapshots). 我为Ubuntu 16.04安装了Powershell(我正在运行Ubuntu 16.10),开发了我的PS脚本,并从Powershell命令行对其进行了尝试,并且效果很好(目的是制作VMWare快照)。

I also tried from bash shell, launching like this: 我也尝试从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\""

and it worked fine as well. 而且效果也不错。

However, when I ran this exact same command from within shell_exec(); 但是,当我从shell_exec();运行此完全相同的命令时shell_exec(); in PHP, I first had issues when launching Powershell: 在PHP中,启动Powershell时我首先遇到了问题:

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

That I resolved thanks to this blog post ( http://www.virtuallyghetto.com/2017/01/system-management-automation-configpropertyaccessor-exception-when-launching-powercli-core-in-linux-firstboot-script.html ) by copying my ~/.local/ to /var/www/html/.local/ and specifying export HOME=/var/www/html so that it runs in the right Home (Apache Home) and it can load the PowerCLI modules that live inside. 我通过此博客文章解决了该问题( 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模块住在里面。

This done, I thought it would run my script and everyone would be happy. 完成此操作后,我认为它将运行我的脚本,并且每个人都会感到高兴。 That obviously wasn't the case. 显然不是这样。

So to sum it up, I am now calling the script that way: 综上所述,我现在以这种方式调用脚本:

$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>";

And it seems that Powershell is properly called, but even if I reduce my Powershell script to a simple Write-Host "aaaaa" , or even only Get-Command it seems that no Cmdlets are available. 而且似乎正确调用了Powershell,但是即使我将Powershell脚本简化为简单的Write-Host "aaaaa"或什至只有Get-Command ,似乎也没有Cmdlet可用。

Here is the Output: 这是输出:

<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>

So it looks like something is not loaded or some configuration is not done properly. 因此,似乎未加载某些内容或某些配置未正确完成。 But I can't find out what. 但是我找不到。

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

What could be going on? 可能会发生什么?

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

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

Thanks to MaximoTrinidad from Powershell Team to lead me on this: https://github.com/PowerShell/PowerShell/issues/3494 感谢Powershell团队的MaximoTrinidad指导我: https//github.com/PowerShell/PowerShell/issues/3494

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

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