private void connectButton_Click(object sender, EventArgs e) { connectButton.Enabled = false; connectButton.Text = "Connecting. Please wait..."; connectButton.Refresh(); VCenterUserName = userIDTextbox.Text; VCenterUserPassword = passwordTextbox.Text; VCenterServer = vcenterServerName.Text; try { VCenterClient = new VimClient(); VCenterClient.Connect(String.Format("https://{0}/sdk", vcenterServerName.Text)); VCenterClient.Login(VCenterUserName, VCenterUserPassword); IList<EntityViewBase> dataCenterList = VCenterClient.FindEntityViews(typeof(Datacenter), null, null, null); foreach (Datacenter dc in dataCenterList) { datacenterCombobox.Items.Add(new ComboboxItem(dc.Name, dc.MoRef.Value)); } connectButton.Text = "Connected. Click OK or Select a datacenter and click Import."; button2.Enabled = true; datacenterCombobox.Enabled = true; datacenterCombobox.SelectedIndex = 0; powerStateCombobox.Enabled = true; powerStateCombobox.SelectedIndex = 0; DCMoRef = ((ComboboxItem)datacenterCombobox.SelectedItem).Value; PowerState = powerStateCombobox.SelectedText; button1.Enabled = true; Connected = true; } catch (Exception ex) { connectButton.Text = "Error occurred. Adjust settings and Connect."; connectButton.Enabled = true; } finally { } }
private void button1_Click(object sender, EventArgs e) { lbVM.Items.Clear(); lbHost.Items.Clear(); lbCluster.Items.Clear(); // Connect to the VMware vCenter SDK service Client.Connect("https://" + tbVCS.Text + "/sdk"); Client.Login(tbUN.Text, tbPW.Text); // Get a list of Windows VM's NameValueCollection filter = new NameValueCollection(); filter.Add("Config.GuestFullName", "Windows"); vmlist = Client.FindEntityViews(typeof(VirtualMachine), null, filter, null); //Populate the VM names into the VM ListBox foreach (VirtualMachine vm in vmlist) { lbVM.Items.Add(vm.Name); } // Get a list of ESXi hosts hostlist = Client.FindEntityViews(typeof(HostSystem), null, null, null); // Populate the Host names into the Host ListBox foreach (HostSystem vmhost in hostlist) { lbHost.Items.Add(vmhost.Name); } // Get a list of Clusters clusterlist = Client.FindEntityViews(typeof(ClusterComputeResource), null, null, null); // Populate the Cluster names into the Cluster ListBox foreach (ClusterComputeResource cluster in clusterlist) { lbCluster.Items.Add(cluster.Name); } }
protected List<Datacenter> GetDataCenter(VimClient vimClient, string dcName = null) { // // Get a list of datacenters // List<Datacenter> lstDatacenters = new List<Datacenter>(); List<EntityViewBase> appDatacenters = new List<EntityViewBase>(); try { if (dcName == null) { // // Return all datacenters // appDatacenters = vimClient.FindEntityViews(typeof(Datacenter), null, null, null); } else { // // Return the named datacenter // NameValueCollection dcFilter = new NameValueCollection(); dcFilter.Add("name", dcName); appDatacenters = vimClient.FindEntityViews(typeof(Datacenter), null, dcFilter, null); } foreach (EntityViewBase appDatacenter in appDatacenters) { Datacenter thisDatacenter = (Datacenter)appDatacenter; lstDatacenters.Add(thisDatacenter); } return lstDatacenters; } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }
public List <EntityViewBase> FindEntityViews(Type viewType, ManagedObjectReference beginEntity, NameValueCollection filter, string[] properties) { return(_client.FindEntityViews(viewType, beginEntity, filter, properties)); }
private void btnConnect_Click(object sender, EventArgs e) { clearConnections(); progressBar1.Visible = false; btnConnect.Enabled = false; lbHost.Items.Clear(); String _hostIP = null; String _userID = null; String _password = null; if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Config.xml")) { _appConfig = new XmlDocument(); try { _appConfig.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Config.xml"); } catch (Exception xm) { MessageBox.Show(xm.Message.ToString()); } _hostIP = _appConfig.SelectSingleNode("settings/vmhost").InnerText; _userID = _appConfig.SelectSingleNode("settings/vmhostuserid").InnerText; _password = _appConfig.SelectSingleNode("settings/vmhostpassword").InnerText; _genericUID = _appConfig.SelectSingleNode("settings/vmcommonuserid").InnerText; _genricPWD = _appConfig.SelectSingleNode("settings/vmcommonpassword").InnerText; // Connect to the VMware vCenter SDK service // Client.Connect("https://" + tbVCS.Text + "/sdk"); Client.Connect(@"https://" + _hostIP + "/sdk"); Client.Login(_userID, _password); // Get a list of Windows VM's NameValueCollection Onfilter = new NameValueCollection(); NameValueCollection OFFfilter = new NameValueCollection(); Onfilter.Add("Runtime.PowerState", "PoweredOn"); // filter.Add("Config.GuestFullName", "Windows"); poweredOnvmlist = Client.FindEntityViews(typeof(VirtualMachine), null, Onfilter, null); OFFfilter.Add("Runtime.PowerState", "PoweredOff"); poweredOffvmlist = Client.FindEntityViews(typeof(VirtualMachine), null, OFFfilter, null); listVM.Sorted = true; listBoxPowerOff.Sorted = true; foreach (VirtualMachine vm in poweredOnvmlist) { listVM.Items.Add(vm.Name); PowerOnvmNameList.Add(vm.Name); if (vm.Snapshot != null) { vmSnaps.Add(vm.Name, vm.Snapshot.RootSnapshotList); } } foreach (VirtualMachine vm in poweredOffvmlist) { listBoxPowerOff.Items.Add(vm.Name); if (vm.Snapshot != null) { vmSnaps.Add(vm.Name, vm.Snapshot.RootSnapshotList); } } btnConnect.Enabled = true; //Get a list of ESXi hosts // hostlist = Client.FindEntityViews(typeof(HostSystem), null, null, null); //Populate the Host names into the Host ListBox // foreach (HostSystem vmhost in hostlist) //{ // lbHost.Items.Add(vmhost.Name); //} } else { MessageBox.Show("Config XML is not present"); } }
private List <EntityViewBase> GetAllVirtualMachines() { return(_client.FindEntityViews(typeof(VirtualMachine), null, null, null)); }
protected List<HostSystem> GetHosts(VimClient vimClient, string hostParent = null) { // // Get one or more virtual hosts // List<HostSystem> lstHosts = new List<HostSystem>(); List<EntityViewBase> appHosts = new List<EntityViewBase>(); try { if (hostParent == null) { // // Get all the hosts // appHosts = vimClient.FindEntityViews(typeof(HostSystem), null, null, null); } else { // // Get all the hosts in a cluster // NameValueCollection hostFilter = new NameValueCollection(); hostFilter.Add("parent", hostParent); appHosts = vimClient.FindEntityViews(typeof(HostSystem), null, hostFilter, null); } if (appHosts != null) { foreach (EntityViewBase appHost in appHosts) { HostSystem thisHost = (HostSystem)appHost; lstHosts.Add(thisHost); } return lstHosts; } else { return null; } } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }
protected List<ResourcePool> GetResPools(VimClient vimClient, string ClusterMoRefVal) { // // Get resource pools from a specific cluster // List<ResourcePool> lstResPools = new List<ResourcePool>(); NameValueCollection clusterFilter = new NameValueCollection(); clusterFilter.Add("parent", ClusterMoRefVal); try { List<EntityViewBase> arrResPools = vimClient.FindEntityViews(typeof(ResourcePool), null, clusterFilter, null); if (arrResPools != null) { foreach (EntityViewBase arrResPool in arrResPools) { ResourcePool thisResPool = (ResourcePool)arrResPool; lstResPools.Add(thisResPool); } return lstResPools; } else { return null; } } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }
protected List<ClusterComputeResource> GetClusters(VimClient vimClient, string clusterName = null) { // // Get one or more clusters // List<ClusterComputeResource> lstClusters = new List<ClusterComputeResource>(); List<EntityViewBase> appClusters = new List<EntityViewBase>(); try { if (clusterName == null) { // // Get all the clusters // appClusters = vimClient.FindEntityViews(typeof(ClusterComputeResource), null, null, null); } else { // // Get a specific cluster // NameValueCollection clusterFilter = new NameValueCollection(); clusterFilter.Add("name", clusterName); appClusters = vimClient.FindEntityViews(typeof(ClusterComputeResource), null, clusterFilter, null); } if (appClusters != null) { foreach (EntityViewBase appCluster in appClusters) { ClusterComputeResource thisCluster = (ClusterComputeResource)appCluster; lstClusters.Add(thisCluster); } return lstClusters; } else { return null; } } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }
protected List<VirtualMachine> GetVirtualMachines(VimClient vimClient, Datacenter selectedDC = null, string vmName = null) { // // Get a list of virtual machines // List<VirtualMachine> lstVirtualMachines = new List<VirtualMachine>(); NameValueCollection vmFilter = new NameValueCollection(); ManagedObjectReference DcMoRef = new ManagedObjectReference(); if (vmName != null) { // // A specific virtual machine // vmFilter.Add("name", vmName); } else { vmFilter = null; } if (selectedDC != null) { // // A specific datacenter // DcMoRef = selectedDC.MoRef; } else { DcMoRef = null; } try { // // If DcMoRef and vmFilter are null return all Vm's, otherwise return specific vm's // List<EntityViewBase> appVirtualMachines = vimClient.FindEntityViews(typeof(VirtualMachine), DcMoRef, vmFilter, null); if (appVirtualMachines != null) { foreach (EntityViewBase appVirtualMachine in appVirtualMachines) { VirtualMachine thisVirtualMachine = (VirtualMachine)appVirtualMachine; lstVirtualMachines.Add(thisVirtualMachine); } return lstVirtualMachines; } else { return null; } } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }
protected List<Network> GetPortGroups(VimClient vimClient, Datacenter selectedDC = null, string pgName = null) { // // Get a list of Portgroups // List<Network> lstPortGroups = new List<Network>(); NameValueCollection pgFilter = new NameValueCollection(); ManagedObjectReference DcMoRef = new ManagedObjectReference(); if (pgName != null) { // // Name of a specific portgroup // pgFilter.Add("name", pgName); } else { pgFilter = null; } if (selectedDC != null) { // // A specific datacenter // DcMoRef = selectedDC.MoRef; } else { DcMoRef = null; } try { // // If DcMoRef and pgFilter are null return all portgroups, otherwise return the selected portgroup // List<EntityViewBase> appPortGroups = vimClient.FindEntityViews(typeof(Network), DcMoRef, pgFilter, null); if (appPortGroups != null) { foreach (EntityViewBase appPortGroup in appPortGroups) { Network thisPortGroup = (Network)appPortGroup; lstPortGroups.Add(thisPortGroup); } return lstPortGroups; } else { return null; } } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }
protected List<Datastore> GetDataStore(VimClient vimClient, Datacenter selectedDC = null, string dsName = null) { // // Get a list of datastores from a specific datacenter // List<Datastore> lstDatastores = new List<Datastore>(); NameValueCollection dsFilter = new NameValueCollection(); ManagedObjectReference DcMoRef = new ManagedObjectReference(); if (dsName != null) { // // The name of a specific datastore // dsFilter.Add("name", dsName); } else { dsFilter = null; } if (selectedDC != null) { // // A specific datacenter to get datastores from // DcMoRef = selectedDC.MoRef; } else { DcMoRef = null; } try { // // if DcMoref and dsFilter are empty return all datastores // List<EntityViewBase> appDatastores = vimClient.FindEntityViews(typeof(Datastore), DcMoRef, dsFilter, null); if (appDatastores != null) { foreach (EntityViewBase appDatastore in appDatastores) { Datastore thisDatastore = (Datastore)appDatastore; lstDatastores.Add(thisDatastore); } return lstDatastores; } else { return null; } } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }
protected List<Datacenter> GetDcFromCluster(VimClient vimClient, string clusterParent) { // // Get a datacenter based on the cluster // List<Datacenter> lstDataCenters = new List<Datacenter>(); NameValueCollection parentFilter = new NameValueCollection(); parentFilter.Add("hostFolder", clusterParent); try { // // Get a specific datacenter based on parentFilter // List<EntityViewBase> arrDataCenters = vimClient.FindEntityViews(typeof(Datacenter), null, parentFilter, null); if (arrDataCenters != null) { foreach (EntityViewBase arrDatacenter in arrDataCenters) { Datacenter thisDatacenter = (Datacenter)arrDatacenter; lstDataCenters.Add(thisDatacenter); } return lstDataCenters; } else { return null; } } catch (VimException ex) { // // VMware Exception occurred // txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation."; Error_Panel.Visible = true; return null; } }