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

时间:2017-04-06 09:19:35

标签: php linux powershell

我正在尝试从webapp调用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\""

它也运行良好。

但是,当我在PHP中shell_exec();内运行这个完全相同的命令时,我首先遇到启动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

可能发生什么事?

1 个答案:

答案 0 :(得分:0)

我正在使用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