简体   繁体   中英

Calling Powershell from PHP script no commands available on Ubuntu

I am trying to call a Powershell script from a webapp.

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

I also tried from bash shell, launching like this:

/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(); in PHP, I first had issues when launching 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.

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.

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

Upgrading to powershell_6.0.0-alpha.17-1ubuntu1.16.04.1_amd64.deb resolved the problem.

Thanks to MaximoTrinidad from Powershell Team to lead me on this: https://github.com/PowerShell/PowerShell/issues/3494

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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