Thursday 14 August 2014

How to Export your VM Inventory list from vCenter

How to Export your VM Inventory list from vCenter



Steps to Export your VM Inventory list from VMware vCenter

1- Open the vSphere Client and connect to your vCenter
2- From the top menu go to View ==> Inventory ==> VMs & Templates
3- From the inventory list on the left side choose the VMs you want to include in your inventory export or choose a top level folder that includes the VMs you want to export
4- Choose the Virtual Machines tab from the the tabs on the top of the right side.
5- You might want to modify the filter to include other items like Virtual machines IPs, Hostname, & the similar.
6- From the top menu go to File => Export => Export list.
7- Choose the format you want to save the list into (CSV, Html, & XML are some of the available format).

Editing VM Annotation Notes via VMware vSphere PowerCLI

Yesterday, a few of my fellow co-workers approached me with a problem.  They asked if I would be interested in figuring out a way to update the Notes: field within the annotations section on the summary tab of each VM we have in our server environment.  
For those of you unfamiliar with VM Annotations, its a simple box that looks like this:
Pretty interesting, right?

Enter PowerCLI, a plugin for PowerShell that enables scripting for vCenter.  In some brief searching online, I found many forum posts, blog posts, and VMware KB topics highlighting some advanced scripting features within vCenter that are offered through PowerCLI (I hope to cover PowerCLI in more detail in future posts, for now we’ll get back to this one).  However, all I wanted to do was modify some annotations.  
I began to narrow my searches and came across a few articles, including this site: http://www.cassese.net/?p=34I found the information to be very useful, but it only covered scripting entries for the custom attributes. Nothing about the Notes: field.  
I kept searching.. and searching.. and after an hour and a half, I had two sources that shared a common characteristic.
The first source: http://communities.vmware.com/thread/195115
A single post on the VMware forums (out of the many I found) seemed to address the same goal as myself. These two lines of code caught my eye:
$vmcSpec.config = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmcSpec.config.annotation = “whatever textyou want in the notes field” 
There it was! All I had to do was create a new VirtualMachineConfigSpec object. Now I had to figure out how to do that.  
Luckily the second source held the answer: http://blogs.vmware.com/vipowershell/2008/04/index.html

This is the final script I came up with, albeit I hope to modify it somewhat more (csv file input for starters), it’s functional nonetheless.
# Input array, includes all VM names within vCenter you wish to edit.
$vms = "vm1","vm2";

# Loop through all VMs within vCenter
ForEach ($vmname in $vms) {
 # Create variables for the current VM and its "Contact" and "Description" which are custom attribute fields.
 $vm = Get-VM -Name $vmname; 
 $contact = $vm | Get-Annotation -CustomAttribute Contact;
 $description = $vm | Get-Annotation -CustomAttribute Description;


 $vm | %{Get-View $_.ID} | % {$spec = New-Object VMware.Vim.VirtualMachineConfigSpec; 
 # Edit the annotation notes field 
 $spec.Annotation = ""+$contact.name+":`t"+$contact.value+"`n`n"+$description.name+":  "+$description.value;
 # Re-configure the current VM to apply changes
 Get-View($_.ReconfigVM_Task($spec))}
}

Quick Update: I posted my script on the VMware PowerCLI forums,http://communities.vmware.com/message/1787432#1787432, and LucD was kind enough to suggest using Set-VM -Description as a simpler way of editing the Notes: field.  I did and it turns out this method was much easier.  The new code is below.
Updated Code:
# Input array, includes all VM names within vCenter you wish to edit.
$vms = "vm1","vm2";

# Loop through all VMs within vCenter
ForEach ($vmname in $vms) 
{
     # Create variables for the current VM and its "Contact" and "Description" which are custom attribute fields.  
     # Also create a string $note to use with Set-VM -Description
     $vm = Get-VM -Name $vmname; 
     $contact = $vm | Get-Annotation -CustomAttribute User;
     $description = $vm | Get-Annotation -CustomAttribute Description;
     $note = $contact.name+":`t"+$contact.value+"`n`n"+$description.name+":`t"+$description.value;

     Set-VM -VM $vm -Description $note -Confirm:$false;
}
For more information on PowerCLI, the VMware blog provides a good intro and installation guide here: http://blogs.vmware.com/vipowershell/2011/06/back-to-basics-part-1-installing-powercli.html