private HostDatastoreBrowserSearchResults GetDatastoreSearchResults(string datastorePath, VimClientlContext ctx) { HostDatastoreBrowserSearchResults browserSearchResults = (HostDatastoreBrowserSearchResults)null; ManagedObjectReference[] managedObjects = this.GetManagedObjects(new string[1] { "browser" }); HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); VmDiskFileQuery vmDiskFileQuery = new VmDiskFileQuery(); vmDiskFileQuery.details = new VmDiskFileQueryFlags(); vmDiskFileQuery.details.capacityKb = true; searchSpec.details = new FileQueryFlags(); searchSpec.details.fileSize = true; searchSpec.details.fileOwner = false; searchSpec.details.fileOwner = false; searchSpec.details.fileType = true; searchSpec.details.fileOwnerSpecified = true; searchSpec.query = new FileQuery[2] { (FileQuery) new FolderFileQuery(), (FileQuery)vmDiskFileQuery }; Task task = new Task(this.VcService, this.VcService.Service.SearchDatastore_Task(managedObjects[0], datastorePath, searchSpec)); string op = "Search Datastore"; VimClientlContext rstate = ctx; task.WaitForResult(op, rstate); string[] properties1 = new string[1] { "info.result" }; Dictionary <string, object> properties2 = task.GetProperties(properties1); if (properties2.ContainsKey("info.result")) { browserSearchResults = (HostDatastoreBrowserSearchResults)properties2["info.result"]; } return(browserSearchResults); }
public void GetVirtualDiskFilesForHost() { try { _service = ecb.getConnection().Service; _sic = ecb.getConnection().ServiceContent; ArrayList supportedVersions = VersionUtil.getSupportedVersions(ecb.get_option("url")); ManagedObjectReference hmor = _service.FindByIp(ecb.getConnection().ServiceContent.searchIndex, null, ecb.get_option("hostip"), false); if (hmor == null) { Console.WriteLine("Unable to find host with IP : " + ecb.get_option("hostip") + " in Inventory"); } else { if (VersionUtil.isApiVersionSupported(supportedVersions, "2.5")) { Object[] datastores = getProperties(hmor, new String[] { "datastore" }); Console.WriteLine("Searching The Datastores"); ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[]; foreach (ManagedObjectReference dstore in dstoreArr) { ManagedObjectReference dsBrowser = ecb.getServiceUtil().GetMoRefProp(dstore, "browser"); ObjectContent[] objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" }); DatastoreSummary ds = objary[0].propSet[0].val as DatastoreSummary; String dsName = ds.name; Console.WriteLine(""); Console.WriteLine("Searching The Datastore " + dsName); VmDiskFileQueryFilter vdiskFilter = new VmDiskFileQueryFilter(); String[] type = { "VirtualIDEController" }; vdiskFilter.controllerType = type; Boolean flag = VersionUtil.isApiVersionSupported(supportedVersions, "4.0"); if (flag) { vdiskFilter.thin = true; } VmDiskFileQuery fQuery = new VmDiskFileQuery(); fQuery.filter = vdiskFilter; HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); FileQuery[] arr = { fQuery }; searchSpec.query = arr; //searchSpec.setMatchPattern(matchPattern); ManagedObjectReference taskmor = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec); object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" }, new string[] { "state" }, // info has a property - state for state of the task new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } } ); // Wait till the task completes. if (result[0].Equals(TaskInfoState.success)) { ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" }); TaskInfo tInfo = (TaskInfo)objTaskInfo[0].propSet[0].val;; HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result; int len = searchResult.Length; for (int j = 0; j < len; j++) { HostDatastoreBrowserSearchResults sres = searchResult[j]; FileInfo[] fileArray = sres.file; if (fileArray != null) { for (int z = 0; z < fileArray.Length; z++) { Console.WriteLine("Virtual Disks Files " + fileArray[z].path); } } else { Console.WriteLine("No Thin-provisioned Virtual Disks Files found"); } } } else { Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully"); } } } else { Object[] datastores = getProperties(hmor, new String[] { "datastore" }); Console.WriteLine("Searching The Datastores"); ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[]; foreach (ManagedObjectReference dstore in dstoreArr) { ManagedObjectReference dsBrowser = (ManagedObjectReference) ecb.getServiceUtil().GetMoRefProp(dstore, "browser"); ObjectContent[] objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" }); DatastoreSummary ds = objary[0].propSet[0].val as DatastoreSummary; String dsName = ds.name; Console.WriteLine(""); Console.WriteLine("Searching The Datastore " + dsName); HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); ManagedObjectReference taskmor = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec); object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" }, new string[] { "state" }, // info has a property - state for state of the task new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } } ); // Wait till the task completes. if (result[0].Equals(TaskInfoState.success)) { ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" }); TaskInfo tInfo = (TaskInfo)objTaskInfo[0].propSet[0].val;; HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result; int len = searchResult.Length; for (int j = 0; j < len; j++) { HostDatastoreBrowserSearchResults sres = searchResult[j]; FileInfo[] fileArray = sres.file; for (int z = 0; z < fileArray.Length; z++) { Console.WriteLine("Virtual Disks Files " + fileArray[z].path); } } } else { Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully"); } } } } } catch (Exception e) { ecb.log.LogLine("VirtualDiskFiles : Failed Connect"); throw e; } finally { ecb.log.LogLine("Ended VirtualDiskFiles"); ecb.log.Close(); } }
public Dictionary <string, bool> GetVirtualDisksTypes(string folderName, VimClientlContext ctx) { Dictionary <string, bool> dictionary = new Dictionary <string, bool>((IEqualityComparer <string>)StringComparer.CurrentCultureIgnoreCase); try { string datastorePath = VCService.GetVolumeName(this.Name) + folderName; HostDatastoreBrowserSearchResults[] browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])null; ManagedObjectReference[] managedObjects = this.GetManagedObjects(new string[1] { "browser" }); HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); searchSpec.matchPattern = new string[1] { "*.vmdk" }; searchSpec.searchCaseInsensitive = true; searchSpec.searchCaseInsensitiveSpecified = true; searchSpec.sortFoldersFirst = true; searchSpec.sortFoldersFirstSpecified = true; searchSpec.searchCaseInsensitiveSpecified = true; searchSpec.details = new FileQueryFlags(); searchSpec.details.fileOwner = false; searchSpec.details.fileOwnerSpecified = true; searchSpec.details.fileSize = true; searchSpec.details.modification = true; searchSpec.details.fileType = true; VmDiskFileQuery vmDiskFileQuery = new VmDiskFileQuery(); vmDiskFileQuery.details = new VmDiskFileQueryFlags(); vmDiskFileQuery.details.thin = true; vmDiskFileQuery.details.thinSpecified = true; FileQuery fileQuery = new FileQuery(); searchSpec.query = new FileQuery[1] { (FileQuery)vmDiskFileQuery }; Task task = new Task(this._vimService, this._vimService.Service.SearchDatastoreSubFolders_Task(managedObjects[0], datastorePath, searchSpec)); string op = "Browse Datastore"; VimClientlContext rstate = ctx; task.WaitForResult(op, rstate); string[] properties1 = new string[1] { "info.result" }; Dictionary <string, object> properties2 = task.GetProperties(properties1); if (properties2.ContainsKey("info.result")) { browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])properties2["info.result"]; } if (browserSearchResultsArray != null) { if (browserSearchResultsArray.Length != 0) { foreach (HostDatastoreBrowserSearchResults browserSearchResults in browserSearchResultsArray) { List <VmdkFileInfo> vmdkFileInfoList = new List <VmdkFileInfo>(); if (browserSearchResults.file != null) { foreach (VimApi.FileInfo fileInfo in browserSearchResults.file) { if (fileInfo is VmDiskFileInfo && !dictionary.ContainsKey(fileInfo.path)) { dictionary.Add(fileInfo.path, ((VmDiskFileInfo)fileInfo).thin); } } } } } } } catch (Exception ex) { } return(dictionary); }
public void GetVirtualDiskFilesForHost() { try { _service = ecb.getConnection().Service; _sic = ecb.getConnection().ServiceContent; ArrayList supportedVersions = VersionUtil.getSupportedVersions(ecb.get_option("url")); ManagedObjectReference hmor = _service.FindByIp(ecb.getConnection().ServiceContent.searchIndex, null, ecb.get_option("hostip"), false); if (hmor == null) { Console.WriteLine("Unable to find host with IP : " + ecb.get_option("hostip") + " in Inventory"); } else { if (VersionUtil.isApiVersionSupported(supportedVersions, "2.5")) { Object[] datastores = getProperties(hmor, new String[] { "datastore" }); Console.WriteLine("Searching The Datastores"); ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[]; foreach (ManagedObjectReference dstore in dstoreArr) { ManagedObjectReference dsBrowser = ecb.getServiceUtil().GetMoRefProp(dstore, "browser"); ObjectContent[] objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" }); DatastoreSummary ds = objary[0].propSet[0].val as DatastoreSummary; String dsName = ds.name; Console.WriteLine(""); Console.WriteLine("Searching The Datastore " + dsName); VmDiskFileQueryFilter vdiskFilter = new VmDiskFileQueryFilter(); String[] type = { "VirtualIDEController" }; vdiskFilter.controllerType = type; Boolean flag = VersionUtil.isApiVersionSupported(supportedVersions, "4.0"); if (flag) { vdiskFilter.thin = true; } VmDiskFileQuery fQuery = new VmDiskFileQuery(); fQuery.filter = vdiskFilter; HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); FileQuery[] arr = { fQuery }; searchSpec.query = arr; //searchSpec.setMatchPattern(matchPattern); ManagedObjectReference taskmor = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec); object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" }, new string[] { "state" }, // info has a property - state for state of the task new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } } ); // Wait till the task completes. if (result[0].Equals(TaskInfoState.success)) { ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" }); TaskInfo tInfo = (TaskInfo)objTaskInfo[0].propSet[0].val; ; HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result; int len = searchResult.Length; for (int j = 0; j < len; j++) { HostDatastoreBrowserSearchResults sres = searchResult[j]; FileInfo[] fileArray = sres.file; if (fileArray != null) { for (int z = 0; z < fileArray.Length; z++) { Console.WriteLine("Virtual Disks Files " + fileArray[z].path); } } else { Console.WriteLine("No Thin-provisioned Virtual Disks Files found"); } } } else { Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully"); } } } else { Object[] datastores = getProperties(hmor, new String[] { "datastore" }); Console.WriteLine("Searching The Datastores"); ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[]; foreach (ManagedObjectReference dstore in dstoreArr) { ManagedObjectReference dsBrowser = (ManagedObjectReference) ecb.getServiceUtil().GetMoRefProp(dstore, "browser"); ObjectContent[] objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" }); DatastoreSummary ds = objary[0].propSet[0].val as DatastoreSummary; String dsName = ds.name; Console.WriteLine(""); Console.WriteLine("Searching The Datastore " + dsName); HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); ManagedObjectReference taskmor = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec); object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" }, new string[] { "state" }, // info has a property - state for state of the task new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } } ); // Wait till the task completes. if (result[0].Equals(TaskInfoState.success)) { ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" }); TaskInfo tInfo = (TaskInfo)objTaskInfo[0].propSet[0].val; ; HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result; int len = searchResult.Length; for (int j = 0; j < len; j++) { HostDatastoreBrowserSearchResults sres = searchResult[j]; FileInfo[] fileArray = sres.file; for (int z = 0; z < fileArray.Length; z++) { Console.WriteLine("Virtual Disks Files " + fileArray[z].path); } } } else { Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully"); } } } } } catch (Exception e) { ecb.log.LogLine("VirtualDiskFiles : Failed Connect"); throw e; } finally { ecb.log.LogLine("Ended VirtualDiskFiles"); ecb.log.Close(); } }