Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 165013

Re: Problems setting the EnterLockdownMode Method with SOAP / PHP

$
0
0

I was able to get this to work on my own...

 

If you want to connect to VMware vcenter server or ESXi hosts (5.x0 then do the following;

 

1 )  Download the NuSoap files sourceforge.

 

Here's a sample of how to connect and "do something:.   You will have to use the SDK to create your own XML blocks of code to do what you want.

 

 

---------

// login example via serviceinstance
    require_once('assets/lib/nusoap.php');
    $wsdl = "https://$vcenter/sdk/vimService.wsdl";

    # SOAP settings
     ini_set("soap.wsdl_cache_enabled", "0");

    # SOAP object
    $client = new \SoapClient($wsdl,
     array(
              'trace'=> 1,
              'exceptions' => 0,
              'encoding' => 'UTF-8',
       'location' => "https://$vcenter/sdk" ,
              'passphrase' => ''
          )
    );
 

    // --------------------------------------------------------------------- \\
    $soapmsg['_this'] = new \soapvar('ServiceInstance', XSD_STRING,'ServiceInstance');
    $result = $client->RetrieveServiceContent($soapmsg);
    $service_instance = $result->returnval;
    //print "<textarea cols=120 rows=20>";
    // print_r($result);
    //print "</textarea>";
    // --------------------------------------------------------------------- \\


    // Login to the vCenter Server
    // --------------------------------------------------------------------- \\
    $soapmsg1["_this"] = $service_instance->sessionManager;
    $soapmsg1["userName"] = "$admin_username";
    $soapmsg1["password"] = "$admin_password";
    $result = $client->Login($soapmsg1);
    if ( isset($result->faultstring) ) {
     $err=$result->faultstring;
     return ("Login Failed. " . $err );
    };
    $soap_session = $result->returnval;
    //print "<textarea cols=120 rows=20>";
    // print_r($soap_session);
    //print "</textarea>";
    // --------------------------------------------------------------------- \\


    // Seacrh for and retirn the ESXi Host Object
    // --------------------------------------------------------------------- \\
    $soapmsg2["_this"] = $service_instance->searchIndex;
    $soapmsg2["dnsName"]="$esxi_hostname";
    $soapmsg2["vmSearch"]='0';
    $result = $client->FindByDnsName($soapmsg2);
    if ( isset($result->faultstring) ) {
     $err=$result->faultstring;
     return ("Search for Hostname Failed. " . $err );
    };
    $host_entity = $result->returnval;
    // print "<textarea cols=120 rows=20>";
    //  print_r($host_entity);
    // print "</textarea>";
    // --------------------------------------------------------------------- \\


    // Disable the lockdownMode
    // --------------------------------------------------------------------- \\
    $soapmsg3['_this'] = $host_entity;
    $result = $client->ExitLockdownMode($soapmsg3);
    if ( isset($result->faultstring) ) {
     $err=$result->faultstring;
     //return ("ExitLockDownMode Failed. " . $err );
    };
    //print "<textarea cols=120 rows=5>";
    //  print_r($result);
    //print "</textarea>";
    // --------------------------------------

 

 

That's it... for  connecting to the vcenter server and executing a method.

 

 

 

Now if you want to do queries you have to add blocks of XML code.   I did the following against the ESXi host.  But I guess it should also work with the vcenter server.

 

----------------------------------

 

function get_path($client,$objectid,$type,$path)

  {

      $xml = "

             <specSet>

               <propSet>

                 <type>$type</type>

                 <pathSet>$path</pathSet>

               </propSet>

               <objectSet>

                 <obj type='$type'>".$objectid."</obj>

               </objectSet>

             </specSet>";

        $soapmsg = NULL; // Reset the $soapmsg array..

     $soapmsg["_this"] = array( "_" => 'propertyCollector', "type" => "PropertyCollector");

        $soapmsg["specSet"] =  new \SoapVar($xml,XSD_ANYXML);

 

        return $client->RetrieveProperties($soapmsg);

  }

 

$rtr=get_path($client,$service_instance->rootFolder->_ ,'Folder','childEntity');

  $datacenter_ = $rtr->returnval->propSet->val->ManagedObjectReference->_; // This returns the DATACENTER NAME

 

$rtr=get_path($client,$datacenter_,'Datacenter','hostFolder');

  $hostFolder_ = $rtr->returnval->propSet->val->_; // This returns the hostFolder that holds the CLUSTER NAME

 

  $rtr=get_path($client,$hostFolder_,'Folder','childEntity');

  $cluster_ = $rtr->returnval->propSet->val->ManagedObjectReference->_; // This returns CLUSTER NAME

 

--------------------------------------

 

 

 

It took me forever to find out how to connect to VMware using PHP.  This information should be more readily available since PHP has been around for A-bazillion years.

 

Good luck!


Viewing all articles
Browse latest Browse all 165013

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>