Home > vmware, vsphere > Import–Export VM custom attributes from vCenter with PowerCLI

Import–Export VM custom attributes from vCenter with PowerCLI

Some time ago, during a migration, I needed to migrate all VM custom attributes from a cluster in a first vCenter to another one.

As easy (and boring…) as is to just retype the attributes value, there was over 400 VMs, and I didn’t want to do this by hand.

So…PowerCLI to the rescue! Here the two little script I used for this task.

 

Export

First lets connect to our source vCenter and export all custom attributes:

Connect-VIserver -server VCsource.vlab.local

$attList = @()
$attList = Get-CustomAttribute -TargetType VirtualMachine
$attList | Export-Csv “.\exported-attributes.csv” –NoTypeInformation

then lets export all attributes values and disconnect:

$valueList =@()
Get-cluster CL01 | get-vm | % {
for($i = 0; $i -lt $_.CustomFields.Count; $i ++ ){
$row = “” | Select VMname, FieldKey, FieldValue
$row.VMname = $_.Name
$row.FieldKey = $_.CustomFields.Keys[$i]
$row.FieldValue = $_.CustomFields.Values[$i]
$valueList += $row
}
}
$valueList | Export-Csv “.\exported-attributes-value.csv” -NoTypeInformation

 

Disconnect-VIServer –server “*” -Confirm:$False

 

Import

Ok, now we have all vm moved to the destination vCenter and is time to import all attributes.

First connect to destination vCenter and re-create attributes:

Connect-VIserver -server VCdest.vlab.local

$attList = @()
$attList = Import-Csv “.\exported-attributes.csv”
foreach ($CustAttrib in $attList){
New-CustomAttribute -Name $CustAttrib.Name -TargetType $CustAttrib.TargetType
}

and, at last, lets import all attribute values and disconnect:

$NewAttribs = Import-Csv “.\exported-attributes-value.csv”
foreach($line in $NewAttribs){
# if value is empty, skip it (just to have all the import go faster)
if( $line.FieldValue -ne “”) {
Set-CustomField -Entity (get-vm $line.VMName) -Name $line.FieldKey -Value $line.FieldValue -confirm:$false
}
}

Disconnect-VIServer “*” -Confirm:$False

 

Easy peasy, doesn’t it?  Occhiolino

Categories: vmware, vsphere Tags: ,
  1. 29 Jul 2014 at 21:29

    For this to work in a non-clustered environment, I had to do the following:

     Get-cluster CL01 | get-vm | % { 

    I had to remove “Get-cluster CL01 |”
    leaving

     get-vm | % { 

    In other words,
    $valueList =@()
    get-vm | % {
    for($i = 0; $i -lt $_.CustomFields.Count; $i ++ ){
    $row = “” | Select VMname, FieldKey, FieldValue
    $row.VMname = $_.Name
    $row.FieldKey = $_.CustomFields.Keys[$i]
    $row.FieldValue = $_.CustomFields.Values[$i]
    $valueList += $row
    }
    }
    $valueList | Export-Csv “.\exported-attributes-value.csv” -NoTypeInformation

    Aside from that, everything works great. Thanks!

  1. No trackbacks yet.

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

%d bloggers like this: