[英]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.