Page 1 of 1

Unable to execute PowerCLI from PHP Script

Posted: Sat Dec 06, 2014 2:44 am
by jagannath79
Hi All,

I am trying to connect to my vsphere server and collect info about VMs my code:

Code: Select all

<?php
$vm = $_POST['search'];

$getvm ="Get-VM $vm | Format-List *";

$stats ="Connect-VIServer x.xxx.xxx.xxx -User xxx -Password xxxx"| out-null; write 'VM Info'; $getvm";


$statscli = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -psc \"C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\vim.psc1\" -Command \"{$stats}\"";


$query = shell_Exec($statscli);
echo ('<pre>');
echo $query;
echo ('<pre>');

?>
I am getting the following error:

x86 : The term 'x86' 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 line:1 char:24
+ -psc C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1
-Com ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFound
Exception
+ FullyQualifiedErrorId : CommandNotFoundException

VM Info
Get-VM : The term 'Get-VM' 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 line:1 char:190
+ ... ite 'VM Info'; Get-VM aaa | Format-List *
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-VM:String) [], CommandNotFo
undException
+ FullyQualifiedErrorId : CommandNotFoundException

Any help on this is great for me.. stuck on a critical project..

Thanks

Re: Unable to execute PowerCLI from PHP Script

Posted: Sat Dec 06, 2014 12:37 pm
by Weirdan
have you tried to echo the $statscli variable and execute it in cmd window?

Few tips:
The powershell version I have expects the -psc argument to go first. Yours might be no different.
Modern windows shells accept forward slash (/) as a directory separator. If you use forward slash, you wouldn't need to escape it.