Home > Blogs > VMware PowerCLI Blog


Moving custom fields from one vCenter to another.

If you’re looking at upgrading to vSphere, one thing that may come in handy is an easy and flexible way to move custom fields from your old vCenter to your new one. With PowerCLI this is quite easy… except for one thing: there is no Get-CustomField cmdlet! The why is a long story but it turns out it’s pretty easy to write just such a thing. In fact here it is:













Not too hard at all, really. So how do we use this? One nice feature of PowerCLI that often gets overlooked is its ability to connect to multiple vCenters at once and manage them all. We can take advantage of this to connect to both our old vCenter and our new one, and move the values directly between them. The way this works is to store the output of Connect-VIServer in a variable, then pass this variable to later cmdlets to specify the server you want to manage. For example:


This next bit of code will take all custom fields on all VMs in vcserver1 and transfer them to vcserver2. This assumes the VMs already exist in both locations.






That’s really all there is to it. If you need to transfer custom fields on more than just VMs, say maybe you also have fields on ESX hosts, you can use a more general bit of code that will do this for all types of objects. I’ve added that example to my script repository and you can download it here.

3 thoughts on “Moving custom fields from one vCenter to another.

  1. Richo

    Dude your code didn’t work. “$vm” is returning $null and I am guessing it is because I have more than one custom field on each VM. The code probably works if you only have one custom field. I updated your code to reflect this. If you REMOVE your code where the loop goes through each field, and replace it with the following…

    foreach ($vm in $vms)
    {
    foreach ($field in $fields)
    {
    if ($vm.Name -eq $field.EntityName)
    {
    Set-Customfield -Entity $vm -Name $field.Key -Value $field.Value
    }
    }
    }

    You’re welcome.

    Reply
  2. Vigor Bio

    Have you ever thought about creating an ebook or guest
    authoring on other blogs? I have a blog based upon on the same topics
    you discuss and would really like to have you share
    some stories/information. I know my subscribers would appreciate your work.

    If you’re even remotely interested, feel free to send
    me an email.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*