Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取vSphere主机名而不是主机id?_Php_Soap_Vmware - Fatal编程技术网

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

Php 如何获取vSphere主机名而不是主机id?,php,soap,vmware,Php,Soap,Vmware,我有一个脚本(如下所示),它连接到vCenter并提取所有虚拟机及其当前所在的主机。唯一的问题是它只给我主机ID而不是名字。是否有任何方法可以同时获取名称或在获取id后查找名称 <?php class soapclientd extends soapclient { public $action = false; public function __construct($wsdl, $options = array()) {

我有一个脚本(如下所示),它连接到vCenter并提取所有虚拟机及其当前所在的主机。唯一的问题是它只给我主机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>";
?>