wiki.getshifting.com

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


customattributesvcenter

Export and Import Custom Attributes in vCenter

Summary: When going from one vCenter to another it would be nice if you could just export all of your custom attributes to a csv file and then import them again! Powershell makes it happen again.
Date: Around 2013
Refactor: 21 February 2025: Checked links and formatting.

Note that you don't have to manually create the custom attributes when you import them, they will be created automatically.

Export Custom Attributes

This is the script for exporting the custom attributes:

$timestamp = Get-Date -format "yyyyMMdd-HH.mm"
$startdir = "D:\sjoerd"
$exportfile = "$startdir\vm-custom-attributes-$timestamp.csv"
 
$vms = Get-VM
$Report =@()
    foreach ($vm in $vms) {
        $row = "" | Select Name, Notes, DR, "DR Value", DRStorage, "DRStorage Value", Cluster, "Cluster Value", "DataCatagory", "DataCatagory Value"
        $row.name = $vm.Name
        $row.Notes = $vm.Notes
        $customattribs = $vm | select -ExpandProperty CustomFields
        $row.DR = $customattribs[0].Key
        $row."DR Value" = $customattribs[0].value
        $row.DRStorage = $customattribs[1].Key
        $row."DRStorage Value" = $customattribs[1].value
        $row.Cluster = $customattribs[2].Key
        $row."Cluster Value" = $customattribs[2].value
        $row."DataCatagory" = $customattribs[3].Key
        $row."DataCatagory Value" = $customattribs[3].value
        $Report += $row
    }
 
$report | Export-Csv "$exportfile" -NoTypeInformation

Import Custom Attributes

And this is the script to import the custom attributes again from the (modified) csv file.

$startdir = "D:\sjoerd"
$importfile = "$startdir\vm-custom-attributes-adjustedforimport.csv"
 
$NewAttributes = Import-Csv $importfile
 
ForEach ($vm in $NewAttributes){
   Write-Host (get-date -uformat %I:%M:%S) "$vm is being done now..." -ForegroundColor Green
   # Set-VM -vm $vm.Name -Notes $vm.Notes -Confirm:$false
   Set-CustomField -Entity (get-vm $vm.Name) -Name $vm.DR -Value $vm."DR Value" -confirm:$false
   Set-CustomField -Entity (get-vm $vm.Name) -Name $vm.DRStorage -Value $vm."DRStorage Value" -confirm:$false
   # Set-CustomField -Entity (get-vm $vm.Name) -Name $vm.Cluster -Value $vm."Cluster Value" -confirm:$false
   Set-CustomField -Entity (get-vm $vm.Name) -Name $vm."DataCatagory" -Value $vm."DataCatagory Value" -confirm:$false
}
Note that the notes are also listed in the properties of the VM and are therefore kept and transferred to the new vCenter. Also note that the cluster values (ClusterInvariantVMMId) are coming from SCOM and are not required to be transferred.
customattributesvcenter.txt · Last modified: by 127.0.0.1