SHIFT

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

User Tools

Site Tools


customattributesvcenter
Differences

This shows you the differences between two versions of the page.

Link to this comparison view

customattributesvcenter [2013/04/20 12:59] (current)
sjoerd created
Line 1: Line 1:
 += 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:
 +<code powershell>​
 +$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
 +</​code>​
 +
 +== Import Custom Attributes ==
 +And this is the script to import the custom attributes again from the (modified) csv file.
 +<code powershell>​
 +$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
 +}
 +</​code>​
 +
 +> 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.
 +
 +{{tag>​vmware scripts}}
customattributesvcenter.txt ยท Last modified: 2013/04/20 12:59 by sjoerd