Wednesday, January 20, 2016

Importing VM annotation (Attributes) and notes from CSV file into vCenter - Powercli

As in the earlier blog after exporting VM annotations, notes and other useful information to CSV/excel file, It was time to Import those information on new vCenter, This new vCenter has new Esxi Server connected from old vCenter, and when I check Virtual Machine tab at the right side on the esxi, I don't see any Information which I had present from old vCenter.
I created VM annotation attributes, just to show you how data is missing and everything is blank., This how you miss value vm annotation information after moving esxi to another vcenter
This information can be filled with below script.
 function Import-VMAnnotation {  
 <#   
 .SYNOPSIS   
 Import VM information Annotation, Notes into VM Attributes  
 .DESCRIPTION   
 The function set Annotation, Notes on VM into vcenter, you will require Export-VMAnnotation function to export data into CSV file.  
 .NOTES    
 Author: Kunal Udapi   
 http://kunaludapi.blogspot.com   
 .PARAMETER N/a   
 No Parameters Required   
 .EXAMPLE   
 PS> Import-VMAnnotation -CSV c:\Temp\VMCMDB.csv   
 #>   
 [CmdletBinding()]  
 #####################################    
 ## ## Version: 1    
 ## Tested this script on successfully   
 ## 1) Powershell v4    
 ## 2) Windows 8.1  
 ## 3) vSphere 5.5 (vcenter, esxi)  
 ## 4) powercli 6.0  
 #####################################    
 Param (  
   [Parameter(Mandatory=$true, Position=0)]   
   [ValidateNotNullOrEmpty()]   
   [string]$CSV  
 )  
   Begin{  
    if (-not(Get-PSSnapin vmware.vimautomation.core -ErrorAction SilentlyContinue)) {   
     Add-PSSnapin vmware.vimautomation.core   
    } #if   
   } #Begin  
   Process{  
     $vmlist = Import-Csv $CSV  
     $NonRequiredProperties = ""  
     $properties = $vmlist | Get-Member -MemberType NoteProperty | Where-Object {($_.name -ne "VMName") -and ($_.name -ne "PowerState") -and ($_.name -ne "IPAddress") -and ($_.name -ne "FolderPath") -and ($_.name -ne "Notes")} | Select -ExpandProperty Name  
     $CustomAttributes = Get-CustomAttribute -TargetType VirtualMachine  
     foreach ($NotExist in $properties) {  
       if (!($CustomAttributes.Name -contains $Notexist)) {  
         Write-Host -BackgroundColor Yellow -ForegroundColor Black "###Custom Attribute `"$NotExist`" does not exist Creating it###"  
         [void](New-CustomAttribute -Name $NotExist -TargetType VirtualMachine)  
       } #if (-Not($CustomAttributes -contains $Notexist))  
       else {  
         Write-Host -BackgroundColor DarkGreen "###Custom Attribute `"$NotExist`" exists, Skipping it###"  
       } #else (!($CustomAttributes.Name -contains $Notexist))  
     } #foreach ($NotExist in $properties)  
     Foreach ($VMObj in $VMList) {  
       $vm = Get-VM $VMObj.VMName  
       #$VMObj.VMName  
       Write-Host -BackgroundColor DarkGreen "`t###Adding Notes and Annotation on Virtual Machine `"$Name`"###"  
       [void]($vm | Set-VM -Notes $($VMObj.notes) -Confirm:$false)  
       $Name = $vm.name  
       Foreach ($Prop in $properties) {  
         $PropValue = $VMObj.$prop        
           Write-Host -BackgroundColor Yellow -ForegroundColor Black "`t`t###Adding value `"$PropValue`" to Attribute `"$prop`"###"  
           [void]($VM | Set-Annotation -CustomAttribute $Prop -Value $Propvalue)  
       } #Foreach ($Prop in $properties)  
     } #Foreach ($VM in $VMList)  
   } #Process  
   End{  
   } #End  
 }  
Using this script is fairly simple you can copy paste this code in Powershell profile, on how to use powershell profile you can check my another post here., In case if you don,t want to save it in profile, you can copy paste it in text file and rename extension to .ps1. and on the last line of .ps1 file put line Import-VMAnnotation -CSV c:\temp\VMCMDB.csv to execute this function, and execute it in powershell doing dot sourcing file ie: .\script.ps1 (Before executing make sure you have run as administrator powershell and Set-ExecutionPolicy RemoteSigned (or bypass or unrestricted))

This script requires earlier imported annotation CSV file, no modification in csv file required it should be as it is. and execute function with below command.
Import-VMAnnotation -CSV c:\temp\VMCMDB.csv

Once the script is executed you can see results as on the screen. you don't have to even create vm Annotation attribute, this script take care of it.
I can see the all old information on new vCenter under vm annotations and notes.
If you find this article informative please do share the knowledge. 
Other useful articles on importing and exporting VM information

Exporting virtual machine annotation (Attributes) and notes to CSV file - Powercli

Move/Migrate VMs to folder Path on another vCenter - Powercli

Get vCenter VM folder Path from VMs and Templates- Powercli

Importing VM annotation (Attributes) and notes from CSV file into vCenter - Powercli

Import vCenter roles (privileges) - Powercli

Export vcenter roles (privileges)

No comments: