SHIFT

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

User Tools

Site Tools


Sidebar

Recently Changed Pages:

View All Pages


View All Tags


LinkedIn




WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.


Pages with comments

View All Comments

customattributesvcenter

Export and Import Custom Attributes in vCenter

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.

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.
You could leave a comment if you were logged in.
customattributesvcenter.txt · Last modified: 2021/09/24 00:24 (external edit)