thinkjohn
Contributor
Contributor

VMWare.Vim code to find the Base Image of a Virtual Machine

I am using the VMware vSphere PowerCLI VMware.Vim.dll in a VB.Net application.

I am trying to find the base image property of a Virtual Machine.  I can't seem to locate the property in the Virtual Machine Object.

            Dim bSuccess As Boolean = True
            Dim VimClient As VMware.Vim.VimClient
            Dim ServiceContent As VMware.Vim.ServiceContent
            Dim UserSession As VMware.Vim.UserSession
            Dim VirtualMachines As List(Of VMware.Vim.EntityViewBase)
            Dim VirtualMachine As VMware.Vim.VirtualMachine

            Try
                VimClient = New VMware.Vim.VimClient()

                ServiceContent = VimClient.Connect("https://" & SiteName & "/sdk")
                UserSession = VimClient.Login("xxxxxxxx", "xxxxxxxx")

                VirtualMachines = VimClient.FindEntityViews(GetType(VMware.Vim.VirtualMachine), Nothing, Nothing, Nothing)
                For Each EntityViewBase As VMware.Vim.EntityViewBase In VirtualMachines
                    VirtualMachine = CType(EntityViewBase, VMware.Vim.VirtualMachine)



                    'HERE IS WHERE I NEED HELP FINDING THE PROPERTY

                Next


Please let me know if I am on the right track.  I am just looking for the name of the image and the name of the parent vm for reporting purposes.  I have attached an image of the values.  I can see them when I recompose a VM.


Thanks,
John
0 Kudos
7 Replies
LucD
Leadership
Leadership

Not sure what you are trying to find.

A VDI machine is created started from a replica, at least when you are using linked clones in the VDI pool.

And that replica is cloned from a snapshot on your master VM.

Afaik the VirtualMachine object has no information from a VDI instance all the way to the master VM.

You might try the Tasks and Events on the Composer to find  from which VM the replica was cloned.

But I'm not sure you'll find such an event or task


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thinkjohn
Contributor
Contributor

I am looking for properties associated with a VM that are displayed in the VMWare View Administrator.

If I go to the properties screen for a desktop, I can see the base image property that I need.

I have attached a screenshot of the page.

I would like to know if I can get at these properties...

0 Kudos
LucD
Leadership
Leadership

That information is available in the ADAM partition that View uses.

The following script extracts the information.

$viewServer = "<one-of-your-View-servers>" 
$view
= "dc=vdi,dc=vmware,dc=int" $domain = "LDAP://$viewServer/$view"
$root = New-Object System.DirectoryServices.DirectoryEntry $domain $query = New-Object System.DirectoryServices.DirectorySearcher($root) $query.filter = "(&(objectClass=pae-VM))"
$query.FindAll() | %{   New-Object PSObject -Property @{     Name = $_.Properties["pae-displayname"][0]     BaseVM = $_.Properties["pae-svivmparentvm"][0]     BaseSnapshot = $_.Properties["pae-svivmsnapshot"][0]   } }  | Select Name,BaseVM,BaseSnapshot

The script assumes that you run the script under an account that has sufficient authority to access the ADAM partition.

You can use any of the View servers, they can all reach the ADAM partition.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

A_S
Enthusiast
Enthusiast

So doing in .NET would be (much longer than powershell)...

' VB.NET

  Imports System.DirectoryServices

    Sub Main(ByVal args() As String)
        Console.Clear()

        If args.Length < 1 Then
            MsgBox("Please supply required parameter: <yourviewservername>. I.e. VDISERVER01")
            Exit Sub
        End If

        Dim viewServer As String = args(0)
        Dim view As String = "dc=vdi,dc=vmware,dc=int"
        Dim adsPath As String = String.Format("LDAP://{0}/{1}", viewServer, view)

        Dim searchRoot As New DirectoryEntry(adsPath)
        Dim counter As Integer = 0
        'Define the properties to retrieve (use adsi editor against your View ADAM instance to get all required properties)
        Dim attribs As String() = New String() {"pae-DisplayName", "pae-svivmparentvm", "pae-svivmsnaphot"}

        Using searcher As New DirectorySearcher(searchRoot, "(&(objectClass=pae-VM))", attribs, SearchScope.Subtree)
            'In big environments you should use paging
            searcher.PageSize = 1000
            For Each result As SearchResult In searcher.FindAll()
                counter += 1
                For Each s As String In attribs
                    If result.Properties.Contains(s) AndAlso result.Properties(s).Count > 0 Then
                        Console.WriteLine("{0}: {1}", s, result.Properties(s)(0))
                    End If
                Next
            Next
            Console.WriteLine("Retrieved : {0} objects", counter)
        End Using
    End Sub

 

// C#

Using System.DirectoryServices

public void Main(string[] args)
{
Console.Clear();

if (args.Length < 1) {
  Interaction.MsgBox("Please supply required parameter: <yourviewservername>. I.e. VDISERVER01");
  return;
}

string viewServer = args(0);
string view = "dc=vdi,dc=vmware,dc=int";
string adsPath = string.Format("LDAP://{0}/{1}", viewServer, view);

DirectoryEntry searchRoot = new DirectoryEntry(adsPath);
int counter = 0;
//Define the properties to retrieve (use adsi editor against your View ADAM instance to get all required properties)
string[] attribs = new string[] {
  "pae-DisplayName",
  "pae-svivmparentvm",
  "pae-svivmsnaphot"
};

using (DirectorySearcher searcher = new DirectorySearcher(searchRoot, "(&(objectClass=pae-VM))", attribs, SearchScope.Subtree)) {
  //In big environments you should use paging
  searcher.PageSize = 1000;
  foreach (SearchResult result in searcher.FindAll()) {
   counter += 1;
   foreach (string s in attribs) {
    if (result.Properties.Contains(s) && result.Properties(s).Count > 0) {
     Console.WriteLine("{0}: {1}", s, result.Properties(s)(0));
    }
   }
  }
  Console.WriteLine("Retrieved : {0} objects", counter);
}
}

Cheers.

0 Kudos
LucD
Leadership
Leadership

Thanks for providing the VBS alternative.

My VBS skills are rapidly disappearing :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
kkmcat
Contributor
Contributor

Any tips on how I could find out which snapshots of a particular image are NOT being used so I can be safe in deleting them?

0 Kudos
LucD
Leadership
Leadership

It would be a matter of elimination I guess.
Find all the base images and their active snapshot.
Subtract the active snapshot from all the snapshots on each image.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos