-1

I have installed VMware PowerCLI inside ym application server, and if i directly write the following inside windows powershell i will get the resutl i am looking for :-

PS C:\Windows\system32> add-pssnapin VMware.VimAutomation.Core;
PS C:\Windows\system32> Connect-VIServer -Server ***** -User root -Password "*******"
PS C:\Windows\system32> Get-VMHOST

Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Version
----                 --------------- ---------- ------ ----------- -----------   -------------   ------------- -------
**********           Connected       PoweredOn       8        2733       18400          12.894          15.998   5.0.0

but if i try to run the command inside my asp.net mvc visual studio project , as follow:-

 public ActionResult About(string vCenterIp = "****", string vCenterUsername = "****", string vCenterPassword = "****")
        {


var shell = PowerShell.Create();



                string PsCmd = "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" + vCenterIp + "';$vCenterAdmin = '" + vCenterUsername + "' ;$vCenterPassword = '" + vCenterPassword + "';" + System.Environment.NewLine;



                PsCmd = PsCmd + "$VIServer = Connect-VIServer -Server $vCenterServer -User $vCenterAdmin -Password $vCenterPassword;" + System.Environment.NewLine;


                PsCmd = PsCmd + "$VMHosts = Get-VMHost" + System.Environment.NewLine;



                shell.Commands.AddScript(PsCmd);

                var results = shell.Invoke();




                if (results.Count > 0)

then the result will always be zero .. can anyone adivce on this please?

John John
  • 1
  • 72
  • 238
  • 501

1 Answers1

1

How could there be results if you absorb them into a Powershell-local variable, and don't pass through to the pipeline? "$VMHosts = Get-VMHost" This command does not return anything (Get-VMHost is absorbed). Change it to plain Get-VMHost and there will be results.

Vesper
  • 18,599
  • 6
  • 39
  • 61