Migrating vCenter Custom Annotations - Envision Technology Advisors' blog

How can we help? 401-272-6688

The Envision Blog


Migrating vCenter Custom Annotations

About a year ago, I was faced with a unique problem.

I was working on a client environment where an old vCenter server was being replaced (one which we did not want to bring the full configuration forward on), but on which custom annotations had been created, and thus embedded, into the old vCenter database. Migrating them manually would have been a labor-intensive task since there were six custom fields and over 500 VM’s, so I turned to PowerCLI to create a solution to the problem. Before I disconnected my hosts from the old vCenter, I imported their annotations into an object, then populated them in the new vCenter from that object.

The first thing I did was to store the credentials to log into the old vCenter server:
     $cred1 = Get-Credential

Then I logged into the old vCenter (while all the hosts were still connected) using the stored credentials:
     Connect-VIServer -Server <Old_vCenter_IP> -Credential $cred1

From there I created a function to export the annotations, then populated an object with the function:
     function Get-CustomField {
          param($item)
          foreach ($cf in $item.CustomFields) {
               if ($cf.Value) {
                    $field = "" | Select Key, Value, EntityName
                    $field.Key  = $cf.Key
                    $field.Value  = $cf.Value
                    $field.EntityName = $item.Name
                    Write-Output $field
               }
          }
     }
    
     $fields = Get-VM | Foreach { Get-CustomField $_ }

In this example, $fields now holds all our annotation information. Note that you need to be careful not to close the PowerCLI window or the object you just created will be destroyed. Now you can disconnect the hosts from the old vCenter and connect them to the new vCenter. After that has been completed, you need to head back over to PowerCLI, disconnect from the old vCenter, and connect to the new one (perhaps with new credentials too).

Store the credentials to log into the new vCenter server:
     $cred2 = Get-Credential

Disconnect from the old vCenter:
     Disconnect-VIServer

Connect to the new vCenter using the stored credentials:
     Connect-VIServer -Server <New_vCenter_IP> -Credential $cred2

At this point, all that’s left is to populate the new database from the $fields object:
     $vms = Get-VM
     foreach ($field in $fields) {
          $vm = $vms | Where { $_.Name -eq $field.EntityName }
          Set-CustomField -Entity $vm -Name $field.Key -Value $field.Value
     }

Read other blog articles from Envision

How can we help you? Call us today at 401-272-6688