How to copy custom attributes when migrating vmware vcenter to new database
I recently had to move hosts and guests to a new vcenter server as the old server had become corrupt and full of issues.
recently had to move hosts and guests to a new vcenter server as the old server had become corrupt and full of issues.
The current vcenter has a few custom attributes and notes that would not be transferred as part of the move.
So I wanted to use powercli to read the attributes out and put them back.
To export the attributes I used the script below.
You will need to add as many Key Value pairs as you have custom attributes
#load Vmware Module
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer -User 'VMUSER' -Password 'USerPasswd221' -Server 'vcenter1'
$vmlist = get-vm
$Report =@()
foreach ($vm in $vmlist) {
$row = "" | Select Name, Notes, Key, Value, Key1, Value1, Key2, Value2, Key3, Value3
$row.name = $vm.Name
$row.Notes = $vm | select -ExpandProperty Notes
$customattribs = $vm | select -ExpandProperty CustomFields
$row.Key = $customattribs\[0\].Key
$row.Value = $customattribs\[0\].value
$row.Key1 = $customattribs\[1\].Key
$row.Value1 = $customattribs\[1\].value
$row.Key2 = $customattribs\[2\].Key
$row.Value2 = $customattribs\[2\].value
$row.Key3 = $customattribs\[3\].Key
$row.Value3 = $customattribs\[3\].value
$Report += $row
}
$report | Export-Csv "c:\\vms-with-notes-and-attributes.csv" -NoTypeInformationIt should produce a csv file that looks something like this
VMNAME,NOTES,CREATEDATE,CREATOR,DEPLOYDATE,TEAM
vmguest1,note1,12/29/2011,Bob,12/30/2011,Web
vmguest2,note2,12/29/2011,John,12/30/2011,Accounts
vmguest3,note3,12/29/2011,Paul,12/30/2011,DatabaseOnce you have exported the file you need to import it into the new vCenter
again adding Key Value pairs as needed.
#load Vmware Module
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer -User 'VMUSER' -Password 'USerPasswd221' -Server 'vcenter2'
$NewAttribs = Import-Csv "C:\\vms-with-notes-and-attributes.csv"
foreach ($line in $NewAttribs) {
set-vm -vm $line.Name -Description $line.Notes -Confirm:$false
Set-CustomField -Entity (get-vm $line.Name) -Name $line.Key -Value $line.Value -confirm:$false
Set-CustomField -Entity (get-vm $line.Name) -Name $line.Key1 -Value $line.Value1 -confirm:$false
Set-CustomField -Entity (get-vm $line.Name) -Name $line.Key2 -Value $line.Value2 -confirm:$false
Set-CustomField -Entity (get-vm $line.Name) -Name $line.Key3 -Value $line.Value3 -confirm:$false
}Hope this helps someone.
Did you enjoy this article?
Recommend it — Standard Reader surfaces well-loved writing to more readers across the network.