Monday, January 18, 2016

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

As I wanted to retain all the configuration and Information about VMs from previous vCenter and apply the same information on another vCenter - (Migration is about moving from Windows vCenter 5.5 to vCenter application linux 6.0 based for testing purpose), I was doing some Esxi migration to another vCenter and earlier I had written 2 script to Get Virtual machine folder Path from and VMs and Templates and Move them to the exact same folder hierarchy on another vcenter.
For small projects where there is no CMDB (Configuration Management Database) Tool is not available, you can maintain your poor mans CMDB in vCenter to fill up information about VMs and hosts or any other object, and can be used it to track information, I have implemented this process in office and made it mandatory for colleagues. Whenever VM is created, Annotation and Notes must be filled with the information (ask project team). This way I don't have to look out or ask project team about VM info every time I need. This is good for time begin CMDB tools not exist, or tools in implementation phase, and it helped me lot in the past by maintaining this data.

Back to exporting Annotation and Notes, This is the script I written today, to get the information, My annotation consist, Application Info, Contact No and Email of the team or owner of VM, Environment whether Production, Critical. Maintenance Windows (Very useful one), Project and Sub project, Notes consist any setting or any other useful information about VM. In case if your annotation attributes does match the names I have, it doesn't matter, My script can pull the information in nice CSV view.
Once you export the info to CSV you can check Import-VMAnnotation script to import information into new vCenter.

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


This is the script:
 function Export-VMAnnotation {  
   <#   
   .SYNOPSIS   
   Export VM information Folderpath, Annotation Notes  
   .DESCRIPTION   
   The function retrives folderPath, Annotation, Notes from vcenter, it can be exported to csv file.  
   .NOTES    
   Author: Kunal Udapi   
   http://kunaludapi.blogspot.com   
   .PARAMETER N/a   
   No Parameters Required   
   .EXAMPLE   
   PS> Export-VMAnnotation -Datacenter DatacenterName   
   .EXAMPLE   
   PS> Export-VMAnnotation -Datacenter DatacenterName | Export-CSV -Path 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, ValueFromPipelineByPropertyName=$true, Position=0)]   
     [ValidateNotNullOrEmpty()]   
     [Alias("Name")]   
     [string]$DataCenter  
   )  
   Begin {  
     if (-not(Get-PSSnapin vmware.vimautomation.core -ErrorAction SilentlyContinue)) {  
       Add-PSSnapin vmware.vimautomation.core  
     } #if  
     function Get-VMFolderPath {   
      <#   
      .SYNOPSIS   
      Get folder path of Virtual Machines   
      .DESCRIPTION   
      The function retrives complete folder Path from vcenter (Inventory >> Vms and Templates)   
      .NOTES    
      Author: Kunal Udapi   
      http://kunaludapi.blogspot.com   
      .PARAMETER N/a   
      No Parameters Required   
      .EXAMPLE   
      PS> Get-VM vmname | Get-VMFolderPath   
      .EXAMPLE   
      PS> Get-VM | Get-VMFolderPath   
      .EXAMPLE   
      PS> Get-VM | Get-VMFolderPath | Out-File c:\vmfolderPathlistl.txt   
      #>   
      #####################################    
      ## http://kunaludapi.blogspot.com    
      ## Version: 1    
      ## Windows 8.1    
      ## Tested this script on    
      ## 1) Powershell v4    
      ## 2) VMware vSphere PowerCLI 6.0 Release 1 build 2548067    
      ## 3) Vsphere 5.5    
      #####################################    
       Begin {} #Begin   
       Process {   
        foreach ($vm in $Input) {   
         $DataCenter = $vm | Get-Datacenter   
         $DataCenterName = $DataCenter.Name   
         $VMname = $vm.Name   
         $VMParentName = $vm.Folder   
         if ($VMParentName.Name -eq "vm") {   
          $FolderStructure = "{0}\{1}" -f $DataCenterName, $VMname   
          $FolderStructure   
          Continue   
         }#if ($VMParentName.Name -eq "vm")   
         else {   
          $FolderStructure = "{0}\{1}" -f $VMParentName.Name, $VMname   
          $VMParentID = Get-Folder -Id $VMParentName.ParentId   
          do {   
           $ParentFolderName = $VMParentID.Name   
           if ($ParentFolderName -eq "vm") {   
            $FolderStructure = "$DataCenterName\$FolderStructure"   
            $FolderStructure   
            break   
           } #if ($ParentFolderName -eq "vm")   
           $FolderStructure = "$ParentFolderName\$FolderStructure"   
           $VMParentID = Get-Folder -Id $VMParentID.ParentId   
          } #do   
          until ($VMParentName.ParentId -eq $DataCenter.Id) #until   
         } #else ($VMParentName.Name -eq "vm")   
        } #foreach ($vm in $VMList)   
       } #Process   
       End {} #End   
      } #function Get-VMFolderPath    
   } #Begin  
   Process {  
     $VMList = Get-Datacenter $DataCenter | Get-VM  
     foreach ($vm in $VMList) {  
               $Annotation = $vm | Get-Annotation  
       $IPAddress = $vm.extensiondata.guest.ipaddress -Join ", "  
               $FolderPath = $vm | Get-VMFolderPath  
       $InfoObj = New-Object PSObject  
       $InfoObj | Add-Member -MemberType NoteProperty -Name VMNAME -Value $vm.Name  
               $InfoObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress  
       $InfoObj | Add-Member -MemberType NoteProperty -Name PowerState -Value $vm.Powerstate  
       $InfoObj | Add-Member -MemberType NoteProperty -Name FolderPath -Value $FolderPath  
       $InfoObj | Add-Member -MemberType NoteProperty -Name Notes -Value $vm.Notes  
       foreach ($Attr in $Annotation) {  
         $InfoObj | Add-Member -MemberType NoteProperty -Name $($Attr.Name) -Value $Attr.Value  
       }  
               $InfoObj  
     } #foreach $vm  
   } #Process  
   end {}  
 }#function Export-VMAnnotation  
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 Export-VMAnnotation 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))

Once the function executated as below, You will see results on console.
Export-VMAnnotation -Datacenter DatacenterName
If you want to export information to CSV (excel). You can execute
Export-VMAnnotation -Datacenter DatacenterName | Export-CSV -Path C:\Temp\VMCMDB.csv
It pulls below information additionally, and doesn't require any changes in the original script.
VMName,
IPAddress
PowerState
FolderPath (This is my earlier script I have used to pull information)
VM notes
VM annotation and Attribution.


Although I had written sepearte article for folderpath but I wanted to keep all incormation in same file.I hope this is been informative, Stay tuned for my another script on importing VM notes and annotation in another vCenter.

Other useful information on migration

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)

Post a Comment