VMware {code} Community
richard_judd
Contributor
Contributor

Enumerating virtualmachines within a specific cluster, or list of clusters

The problem i'm having is that after successfully enumerating the clusters within a given VC I cant seem to just enumerate through just the virtualmachines on that cluster.

I recieve no error or indication that the code or methods are wrong just it duplicates results for each cluster.

Anyone done this before successfully (of course it looks good when you only have one cluster in your DEV env)

The code snippet in VB.NET

Reply
0 Kudos
2 Replies
Steve_Jin
Expert
Expert

It seems you were using the .Net Toolkit behind the VI Powershell.

I am not quite familiar with VB, but here are two things you can check:

1. the name filter, it limits the vms whose names match the patterns you put there.

2. I think you intended to search all VM in the cluster, but I am not sure the following code is the way to go.

client.FindEntityViews(GetType(ComputeResource), Nothing, Nothing, Nothing)

I checked the devoper guide:http://www.vmware.com/support/developer/windowstoolkit/wintk10/doc/viwin_devg.pdf, which doesn't have API ref on how to use the FindEntityViews(). The way .Net toolkit is designed is much like VI Perl toolkit. So check the API ref there might give you some idea.

Good luck!

Steve JIN, VMware Engineering

Creator of VMware Infrastructure Java API:

VI Java API 2.0 --- 15 times faster than AXIS in loading, 4+ faster in deserialization; only 1/4 of the size required by AXIS. More importantly freedom to redistribute your applications.

Download, Samples, DocWiki, RSS Feed

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
richard_judd
Contributor
Contributor

Got it all working in the end,....

Public Sub getUnassignedVirtualmachines() alAVMAvailableClients.Clear() alAVMSelectedClients.Clear() Dim vmCenter As String = Me.cbx_GBM_BBP_VIC.SelectedItem.ToString Dim client As New VMware.Vim.VimClient If ConnectToVirtualCentre(client, vmCenter) Then Else Exit Sub End If Dim filter As New NameValueCollection filter.Add("Runtime.PowerState", "PoweredOff") filter.Add("name", "^" & Me.cbx_GBM_BBP_DiskSize.SelectedItem.ToString & Me.cbx_GBM_BBP_GEOSVer.SelectedItem.ToString.Replace(".", "")) Dim ClusterList As IList(Of EntityViewBase) = client.FindEntityViews(GetType(ClusterComputeResource), Nothing, Nothing, Nothing) If ClusterList Is Nothing Then 'No Clusters Call DisconnectFromVirtualCentre(client) Exit Sub End If For Each ThisCluster As ClusterComputeResource In ClusterList Dim HostList As IList(Of EntityViewBase) = client.FindEntityViews(GetType(HostSystem), ThisCluster.MoRef, Nothing, Nothing) If HostList Is Nothing Then 'No Hosts Else For Each ThisHost As HostSystem In HostList Dim vmList As IList(Of EntityViewBase) = client.FindEntityViews(GetType(VirtualMachine), ThisHost.MoRef, filter, Nothing) If vmList Is Nothing Then 'No VM's Else For Each vm As VirtualMachine In vmList Dim vmClusterName As String = ThisCluster.Name Dim vmHost As String = ThisHost.Name.ToUpper Dim vmName As String = vm.Name.ToUpper Dim vmGName As String = vm.Guest.HostName Dim vmPath As String = vm.Config.Files.VmPathName.ToUpper Dim vmSerial As String = "VMware-" & ConvertUUID(vm.Config.Uuid.Replace("-", "")) Dim vmGState As String = vm.Guest.GuestState If (vmPath.Split("]")(0) & "];").ToUpper.Contains("_12") Then Dim h As String = "gg" Else Dim jj As String = "gg" End If If vmName.Contains("TEMPLATE") Or vmName.Contains("TMP") Then Else Call addToHashTable(GWDListVMs, vmName, vmCenter & ";" & vmClusterName & ";" & vmHost & ";" & vmPath.Split("]")(0) & "];" & vmName & ";" & vmSerial & ";" & vmPath.Split("]")(1).Split("/")(1)) Me.WriteDataListVMs(Me.dt_ListVMs, vmCenter & ";" & vmClusterName & ";" & vmHost & ";" & vmPath.Split("]")(0) & "];" & vmName & ";" & vmSerial & ";" & vmPath.Split("]")(1).Split("/")(1)) End If Next End If Next End If Next Call DisconnectFromVirtualCentre(client) Me.lbl_GBM_BBP_listed.Text = Me.ug_GBM_BBP_ListVMs.Rows.FilteredInRowCount & " Virtual machine(s) listed" Me.lbl_GBM_BBP_Selected.Text = Me.ug_GBM_BBP_AssignVMs.Rows.FilteredInRowCount & " Virtual machine(s) listed" End Sub

Reply
0 Kudos