Powershell · Virtual Machine · vSphere 5.0

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.

Leave a comment