How to export VM annotations to a file

Newest vSphere versions allow us to create many custom VM description fields on vCenter inventory making our life easier and helping us to indentify what the VM is for, who the responsible is, etc.

Migrating these VM's to another upgraded environment result in losing these important description fields and, believe me, recreate them all is a really tough job when several machines are involved. Hence I finally made a script to export these fields so it could be imported later. 

Fist thing: EXPORTING

· Read VM names from a txt file VMInfoList.txt
· Read All VM Annotation Fields 
· I'll get one row with VM Name, Field Name and Field Value
· Export to csv
##########################################################
# Name: ReadVMDescriptions
# Desc: Exports the VM annotation properties to a CSV file
# Date: 24/01/2013
# Auth: Alberto Ruiz
# https://virtualpad.wordpress.com
##########################################################

# Output File Name
$FileList = "VMInfoList.txt"

$Report= @()
$VMList=Get-Content $FileList

	ForEach($VMname in $VMList)

		{
            $VMdata = Get-VM $VMname
			$VMAnnotations = Get-VM $VMName | Get-Annotation

			Foreach($Annotation in $VMAnnotations)

			{

				$Row = ""| select VMName, FieldName, FieldValue
				$Row.VMName = $VMdata.Name
				$Row.FieldName =  $Annotation.Name
				$Row.FieldValue = $Annotation.Value

				$Report += $Row
			}

		}

$report | export-csv ExportAnnotations.txt

Then, IMPORTING:
· Read VM names, fieldname and value from csv file
· Upgrade VM with data

##########################################################
# Name: WriteVMDescriptions
# Desc: Updates the VM annotation from a CSV file
# Date: 24/01/2013
# Auth: Alberto Ruiz
# https://virtualpad.wordpress.com
##########################################################

$FileList = "ExportAnnotations.txt"
$VMList=Import-CSV $FileList

	ForEach($Line in $VMList)
		{
            Get-Vm $Line.VMname | Set-CustomField -name $Line.FieldName -Value $Line.FieldValue

		}

Check it out and remember: Begin testing with only a few VM to ensure it fits your environment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s