如何获取vSphere主机名而不是主机ID?

时间:2014-08-19 22:14:11

标签: php soap vmware

我有一个脚本(如下所示),它连接到vCenter并拉出所有VM以及它们当前所在的主机。唯一的事情是它只给我主机ID而不是名称。是否有任何方法可以获取名称或在获得ID后查找名称?

<?php
class soapclientd extends soapclient
    {
        public $action = false;
        public function __construct($wsdl, $options = array())
            {
                parent::__construct($wsdl, $options);
            }
        public function __doRequest($request, $location, $action, $version, $one_way = 0)
            {
                $resp = parent::__doRequest($request, $location, $action, $version, $one_way);
                return $resp;
            }
    }
$client = new SoapClient("https://192.168.1.103/sdk/vimService.wsdl", array("trace" => 1, "location"=>"https://192.168.1.103/sdk"));
try
    {
        $request = new stdClass();
        $request->_this = array ('_' => 'ServiceInstance', 'type' => 'ServiceInstance');
        $response = $client->__soapCall('RetrieveServiceContent', array((array)$request));
    } 
catch (Exception $e)
    {
        echo $e->getMessage();
        exit;
    }
$ret = $response->returnval;
try
    {
        $request = new stdClass();
        $request->_this = $ret->sessionManager;
        $request->userName = 'root';
        $request->password = 'vmware';
        $response = $client->__soapCall('Login', array((array)$request));
    } 
catch (Exception $e)
    {
        echo $e->getMessage();
        exit;
    }
$ss1 = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$ss2 = new soapvar(array ('name' => 'DataCenterVMTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$a = array ('name' => 'FolderTraversalSpec', 'type' => 'Folder', 'path' => 'childEntity', 'skip' => false, $ss1, $ss2);
$ss = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$b = array ('name' => 'DataCenterVMTraversalSpec', 'type' => 'Datacenter', 'path' => 'vmFolder', 'skip' => false, $ss);
$res = null;
try
    {
        $request = new stdClass();
        $request->_this = $ret->propertyCollector;
        $request->specSet = array (
            'propSet' => array (
                array ('type' => 'VirtualMachine', 'all' => 0, 'pathSet' => array ('name','runtime.powerState', 'config.hardware.numCPU', 'config.hardware.memoryMB', 'runtime.host')),
            ),
            'objectSet' => array (
                'obj' => $ret->rootFolder,
                'skip' => false,
                'selectSet' => array (
                    new soapvar($a, SOAP_ENC_OBJECT, 'TraversalSpec'),
                    new soapvar($b, SOAP_ENC_OBJECT, 'TraversalSpec'),
                    ),
                )
            );
        $res = $client->__soapCall('RetrieveProperties', array((array)$request));
    } 
catch (Exception $e)
    {
        echo $e->getMessage();
    }
$tvCPU = 0;
$tvRAM = 0;
echo "<table><tr><td width='450px'>Name</td>    <td width='100px'>vRAM (MB)</td><td width='100px'>vCPU</td><td width='100px'>Power State</td><td width='100px'>Host</td> </tr>";
$arrlength=count($res ->returnval);
for($x=0;$x<$arrlength;$x++) 
    {
        $name = $res -> returnval[$x] -> propSet[2]->val;
        $vRam = $res -> returnval[$x] -> propSet[0]->val;
        $vCPU = $res -> returnval[$x] -> propSet[1]->val;
        $pState = $res -> returnval[$x] -> propSet[4]->val;
        $host = $res -> returnval[$x] -> propSet[3]->val->_;
        echo "<tr><td width='450px'>".$name.
        "</td><td>".$vRam.
        "</td><td>".$vCPU.
        "</td> <td>".$pState.
        "</td> <td>".$host.
        "</td></tr>";
        $tvCPU = $tvCPU + $vCPU; 
        $tvRAM = $tvRAM + $vRam; 
    }
echo "<tr><td width='450px'>Total VMs - ".$x."</td><td>". $tvRAM."</td><td>". $tvCPU."</td></tr>";
?>

0 个答案:

没有答案
相关问题